Override the Pro Theme header assignment for specific pages and conditions

Michael BourneUpdated:

Themeco’s Pro Header Builder is pretty amazing. From it’s in depth controls to endless styling options, anyone can make a truly magnificent header. But what do you do when the specific page you’d like to target isn’t available as an assignment? How about when you want to show a specific header for logged in users? Or for different user roles?

This little PHP snippet will do just that. But first, you need to get the post ID of the header you’d like to assign. The easy way to do that is to simply hover over the Edit link in the builder, and take a look at the URL in your status bar. It should look similar to domain.com/x/#/headers/1234

That 1234 is the post ID you need for this snippet. Want a custom header on your search results page? Place this in your child theme’s functions.php file:

add_filter('cs_match_header_assignment', 'custom_search_header');
function custom_search_header($match) {
	if (is_search()) {
		$match = 1234; // the post ID for your header
	}
	return $match;
}

Simple as that!

What else can you do with this code? Well, how about your 404 page?

add_filter('cs_match_header_assignment', 'custom_search_header');
function custom_search_header($match) {
	if (is_404()) {
		$match = 1234; // the post ID for your header
	}
	return $match;
}

How about a different header for logged in users?

add_filter('cs_match_header_assignment', 'custom_search_header');
function custom_search_header($match) {
	if (is_user_logged_in()) {
		$match = 1234; // the post ID for your header
	}
	return $match;
}

And for user roles? Here’s one for authors:

add_filter('cs_match_header_assignment', 'custom_search_header');
function custom_search_header($match) {
	$user = wp_get_current_user();
	if (in_array( 'author', (array) $user->roles )) {
		$match = 1234; // the post ID for your header
	}
	return $match;
}

How about assigning headers to multiple pages?

add_filter('cs_match_header_assignment', 'custom_search_header');
function custom_search_header($match) {
	$user = wp_get_current_user();
	if (is_search()) {
		$match = 1234; // the post ID for your header
	}
	elseif (is_404()) {
		$match = 4567; // the post ID for your header
	}
	return $match;
}

You get the idea. Feel free to mix and match as needed to create the custom header assignments you need for you site.

Have a question or a comment about this article? Click here!
About the Author

Michael Bourne

Facebook

Hey, I'm Michael, and I've been making websites since the mid 90s! These days I focus on Wordpress and would call myself quite handy in PHP, JS, and CSS. Outside of coding and web design, I'm quite fond of craft beer and backpacking around the world.

Share this Tip!

There are 7 comments

Your email address will not be published. Required fields are marked *