Hide WordPress Toolbar for a specific page template

For a project I was working on recently I was using a simplified version of the single page template in order to load specific pieces of content into iframes within a plugin page in the WordPress Admin. This was so I could preview the arrangement of the content in the page layout using a custom layout tool I created.

One of the first things I usually do when creating an account in WordPress is to disable the Toolbar in my user profile so that it doesn't get in the way when trying to work on the layout of the site. So on this current site the page content I was loading into the iframes looked fine, however, whilst previewing it on someone else's WordPress account which did not have the Toolbar disabled I realised that each one of my iframes had the toolbar showing and was then pushing the iframe content down.

Whilst I knew it was possible to disable the toolbar programmatically in this case I only wanted it hidden for the template used for these specific iframes but left available if other WordPress users wanted to use it. The first part of the puzzle was the code to disable the toolbar:

add_filter('show_admin_bar', '__return_false');

Adding this filter via either an appropriate action such as 'after_setup_theme' would generally do the trick, but for my use I needed to be able to trigger it only for the page template loading into the iframe. Conveniently in my use I was actually using a custom URL parameter to trigger the simplified single page template, I had a function in my plugin which checks for this URL parameter and then displays my simplified page template (single-basic.php instead of the regular single.php page) so this gave me a good place to disable the toolbar, so I added the 'add_filter...' line above into this function:

function my_basic_template($template) {
	global $wp;
	// get url parameter from page query,
	// i.e. mysite.com/mypage/?basic_template=true
	if ($wp->query_vars['basic_template']) {
                // Disable Toolbar
		add_filter('show_admin_bar', '__return_false');
                // Get rid of the 32px whitespace
		remove_action('wp_head', '_admin_bar_bump_cb');
		
		return get_template_directory() . '/single-basic.php';
	} else {
		return $template;
	}
}
add_filter('single_template', 'my_basic_template');

After adding the 'add_filter...' line to my code it did indeed disable the toolbar for those pages but unfortunately it left a 32px white band at the top where the toolbar would have been displayed, however a quick bit of googling came up with the following 'remove_action' function which gets rid of that whitespace:

remove_action('wp_head', '_admin_bar_bump_cb');

So, with that all in place I now had iframes with content in them and no toolbar in sight! Adding the same two lines in other contexts should also work, either in an 'after_setup_theme' action or other appropriate WordPress hook. It could also be done based on other options such as custom post type etc.


Digital Ocean

Meta

Tags: wordpress,toolbar,web development

Originally published on 2015-02-05 18:17:45 by Rick Curran

Last edited on 2015-02-05 18:20:40 by Rick Curran

Permalink: http://suburbia.org.uk/blog/2015/02/05/181745.html

Visitor Comments:

There are currently no Visitor Comments for this post.

Leave a comment...

Comment Preview:




Digital Ocean