[wp-hackers] Detecting and fixing non-compliant themes

Dan Kuykendall dan at kuykendall.org
Fri Dec 22 01:11:24 GMT 2006


For podPress I have to hook into the wp_head() call which should be in
the header.php and the wp_footer() in the footer.php

However, I have been getting bug reports from users saying that the
player didnt work or that there was some javascript error. These things
would be caused by those two calls not being done in the header.php and
footer.php

To solve this I added a little code to detect theme compatibility and if
non-compliant it will let the user know whats wrong and how to fix it in
the theme.

If anyone is interested, it works like this
1) I have a themeCompatibility setting added to the config. The values
should default to false

2) in callback for switch_themes I set the values to false. This forces
a recheck anytime the user picks a new theme

3) in the callback function I register for wp_head and wp_footer I put
code to set the themeCompatibility settings to true for their respective
 area

4) At the point where I want to inform the user about the
problem/situation I check the themeCompatibility settings and display
appropriate text to explain to the user what the problem is and how to
use it.

Here is code that would work. Its not the exact podPress code, since I
normalized it (removed podPress variable references).

// a couple of globals
$themeCompatibility = get_option('themeCompatibility');
$inAdmin = strpos($_SERVER['REQUEST_URI'], 'wp-admin');

//in the registered callback for wp_head
if(!$inAdmin) {
  if(!$themeCompatibility['themeTested']) {
    $themeCompatibility['themeTested'] = true;
    update_option('themeCompatibility', $themeCompatibility);
  }
  if(!$themeCompatibility['wp_head']) {
    $themeCompatibility['wp_head'] = true;
    update_option('themeCompatibility', $themeCompatibility);
  } else {
    $themeCompatibility['wp_head'] = true;
  }
}


//in the registered callback for wp_footer
if(!$inAdmin) {
  if(!$themeCompatibility['themeTested']) {
    $themeCompatibility['themeTested'] = true;
    update_option('themeCompatibility', $themeCompatibility);
  }
  if(!$themeCompatibility['wp_footer']) {
    $themeCompatibility['wp_footer'] = true;
    update_option('themeCompatibility', $themeCompatibility);
  } else {
    $themeCompatibility['wp_footer'] = true;
  }
}

// Then somewhere along the line (maybe on the themes page) it
// can check to see if the theme is compliant.
// Keep in mind, the user has to visit the main blog page for the checks
// to take place, so it verifies that first.

if(!$themeCompatibility['wp_head'] || !$themeCompatibility['wp_footer']) {
  echo '<div class="wrap">'."\n";
  if($themeCompatibility['themeTested']) {
    echo '	<h2>'.__('Theme Compatibility Problem').'</h2>'."\n";
  } else {
    echo '	<h2>'.__('Theme Compatibility Check Required').'</h2>'."\n";
  }
  echo '	<fieldset class="options">'."\n";
  if($themeCompatibility['themeTested']) {
    echo '		<legend>'.__('Current Theme is not compliant').'</legend>'."\n";
  } else {
    echo '		<legend>'.__('Current Theme needs to be
tested').'</legend>'."\n";
  }
  echo '		<table width="100%" cellspacing="2" cellpadding="5"
class="editform">'."\n";
  echo '			<tr>'."\n";
  echo '				<td>'."\n";
  if($themeCompatibility['themeTested']) {
    echo '				The "'.get_current_theme().'" theme fails to meet
important requirements.<br/><br/>'."\n";
  } else {
    echo '				Wordpress as not yet detected the
"'.get_current_theme().'" theme to be compliant. Please visit your <a
href="'.siteurl().'">main blog page</a> for WordPress to
re-check.<br/><br/>'."\n";
  }

  if(!$themeCompatibility['wp_head']) {
    echo '				The header.php in your theme needs to be calling
wp_head(); before the closing head tag.<br/>'."\n";
    echo '				Change this:<br/>'."\n";
    echo '				<code>&lt;head&gt;</code><br/>'."\n";
    echo '				To this:<br/>'."\n";
    echo '				<code>&lt;?php wp_head();
?&gt;'."<br/>\n".'&lt;head&gt;</code><br/>'."\n";
    echo '				<br/>'."\n";
  }
  if(!$themeCompatibility['wp_footer']) {
    echo '				The footer.php in your theme needs to be calling
wp_footer(); before the closing body tag.<br/>'."\n";
    echo '				Change this:<br/>'."\n";
    echo '				<code>&lt;/body&gt;</code><br/>'."\n";
    echo '				To this:<br/>'."\n";
    echo '				<code>&lt;?php wp_footer();
?&gt;'."<br/>\n".'&lt;/body&gt;</code><br/>'."\n";
  }
  echo '				Look at the default theme files for example.<br/>'."\n";
  echo '				</td>'."\n";
  echo '			</tr> '."\n";
  echo '		</table>'."\n";
  echo '	</fieldset>'."\n";
  echo '</div>'."\n";
}


-- 
Dan Kuykendall (aka Seek3r)
http://www.mightyseek.com

In God we trust, all others we virus scan.
Programmer - an organism that turns coffee into software.


More information about the wp-hackers mailing list