Variable names in WordPress (or why you should never use $category as a variable name)

The great thing about WordPress and PHP is that you can call variables anything you like. The bad thing about WordPress and PHP is that you can call variables anything you like…

Think about these variables:

$post, $category, $template

What do they have in common? Well, they all look like good, descriptive variable names don’t they? What if you changed this to

global $post, $category, $template;

and then put this in a template for a post…?

echo $post; echo $category; echo $template;

Well, you’d get all sorts of interesting things, as $post, $category and $template are all global variables in WordPress.

FYI: there is a (partial) list of WordPress global variables in the Codex.

So if you use these variable names, and you also access global variables with those names… sometimes you’ll be thinking: why doesn’t my code work? And you’ll find out that when you thought you were changing a local variable you were actually changing a global variable, and that affected something else…

See also: Global variables and understanding scope in WordPress.

Learning from themes

A big part of learning how to use WordPress is learning how to do things: one of the best ways to do that is to pull themes and plugins apart and see how they work. There are some great themes and there are some rubbish themes: the more you look at, the more elegant solutions you find. I’ll admit I didn’t really get the big deal behind the twentytwelve theme, and I was distinctly underwhelmed by the underscore theme – though I learned a lot from pulling it to bits.

The upcoming twentythirteen theme is different though – I really like the way it uses post formats and can see how you can abstract a lot of formatting decisions that I used to make on a category basis.

What template am I using? Understanding the WordPress template hierarchy.

Sometimes I find that when I’m developing a theme, changes I make to .php and .css files sometimes don’t seem to have any effect on my theme, no matter how many times I reload the page in a browser or empty the browser’s cache. This is often because WordPress isn’t loading the template file I think it is. This is very true when you’re first developing a brand new site and may not have may posts/tags etc..

The standard advice is to familiarise yourself with WordPress’ template hierarchy – not only does the Codex explain when a template is being used, there is also a helpful flow chart provided by Chip Bennett. The problem is that the documentation often isn’t 100% accurate – for example WordPress won’t use an archive.php template if you’ve only got one post – it will use index.php instead.

A solution to this is to add some code that lets you check what template file WordPress is using: fortunately that value is stored in a global variable called $template. The code below adds a menu item to the WordPress Admin Bar that tells you what template is being used. The function checks if you are on an Admin screen or on your site, and adds an appropriate menu item.

// removes and adds links/menus from the admin bar
function edwp_admin_bar_render() {
	global $wp_admin_bar;

	$wp_admin_bar->add_group( array( 
		'parent' => 'site-name', 
		'id' => 'theme' ) 
		);

	if ( !is_admin() ) {
		global $template;
		$short_path = explode(get_stylesheet_directory(), $template,2); 
		$wp_admin_bar->add_menu( array(
		'parent'=>'theme',
		'id' => 'template',
		'title' => 'Template: '.$short_path[1],
		'href' => ''
		));
	}

	if ( is_admin() ) {
		global $current_screen;
		$wp_admin_bar->add_menu( array(
		'parent'=>'theme',
		'id' => 'cur_screen',
		'title' => 'Admin screen: '.$current_screen->base,
		'href' => ''
		));
	}

}
add_action( 'wp_before_admin_bar_render', 'edwp_admin_bar_render',10);