Changing the heading levels for WordPress widgets

If you want to change the heading levels for widgets in a sidebar, you shouldn’t use actions or filters or css to override WordPress: what you do is change how the sidebar is *defined* when it is first initialised.

If your theme uses a sidebar, it has to register it first. Somewhere in your theme (usually in your functions.php file) you will find a call to the register_sidebar( $args ); function, where $args is either a string or an array that defines how the sidebar is structured.

If there are no arguments for the register_sidebar() call then the defaults are used – this is to use h2 for the widget titles. If you want to use a different heading (or none at all) then you need to set the values for 'before_title' and 'after_title' when you call register_sidebar() like this:

<?php register_sidebar(array(
  'name' => __( 'Right Hand Sidebar' ),
  'id' => 'right-sidebar',
  'description' => __( 'Widgets in this area will be shown on the right-hand side.' ),
  'before_title' => '<h1>',
  'after_title' => '</h1>'
));
?>

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);