Compact ‘pop-out’ search bar placement on specific pages only

This quick tutorial will help you configure and place a pop-out search bar to the sidebar of your site using a custom code – allowing inclusion or exclusion from specific pages.

For generic use please check the Compact Box Layout documentation (includes a video tutorial).

Quick configuration

Make sure to enable the compact box layout mode under the Layout Options -> Compact box layout panel. For this tutorial I recommend the following configuration:

  • Compact layout final width: 320px

  • Compact search box position: fixed (or absolute may also work)

Positioning with a custom code – allowing exclusions/inclusions

Add this custom code to the functions.php in your theme/child theme directory (copy from line 3 only!). Before editing, please make sure to have a full site back-up just in case!

Adjustable variables within the code (lines 7-17):

  • $id -> the search instance ID

  • $exclude_on_pages -> list of page IDs, where the search should not be visible. Leave it empty, if not in use.

  • $include_on_pages -> list of page IDs, where the search should be visible. Leave it empty, if not in use.

  • $exclude_on_archives -> true or false. If true, then then the search will not be visible on post type archives.

  • $exclude_on_tax_archives -> list of taxonomies. The search will not be visible on listed taxonomy archive pages.

  • $exclude_on_front -> true or false. If true, the search will not be visible on the front page.

What is this, and where do I put this custom code?

add_action('wp_footer', 'asp_insert_sc_to_footer', 99999);
function asp_insert_sc_to_footer() {
	// Replace this with the search ID you want to use
	$id = 1;
	// Comma separated list of Pages (or any CPT) where the search should be excluded. Leave it empty to ignore.
	$exclude_on_pages = '1, 2, 3';
	// Comma separated list of Pages (or any CPT) where the search should be excluded. Leave it empty to ignore.
	$include_on_pages = '';
	// Exclude on archive pages?
	$exclude_on_archives = false;
	// Exclude on category (or any taxonomy) archive pages. Comma separated list of taxonomies.
	$exclude_on_tax_archives = 'category, tag';
	// Should it be visible on the front page? true or false
	$exclude_on_front = false;

	// -------- DO NOT TOUCH BELOW ----------
	if ( is_front_page() && $exclude_on_front )
		return false;

	if ( is_archive() && $exclude_on_archives )
		return false;

	$eta = array_filter( explode(",", str_replace(' ', '', $exclude_on_tax_archives)), 'strlen' );
	foreach ( $eta as $_eta ) {
		if ( ($_eta == 'tag' || $_eta == 'post_tag') && is_tag() )
			return false;
		if ( $_eta == 'category' && is_category() )
			return false;
		if ( is_tax($_eta) )
			return false;
	}

	$epa = array_filter( explode(",", str_replace(' ', '', $exclude_on_pages)), 'strlen' );
	$ipa = array_filter( explode(",", str_replace(' ', '', $include_on_pages)), 'strlen' );
	$pid = get_the_ID();
	if ( !is_wp_error($pid) ) {
		if ( in_array($pid, $ipa) || !in_array($pid, $epa) ) {
			echo do_shortcode('[wd_asp id='.$id.']');
		}
	} else {
		echo do_shortcode('[wd_asp id='.$id.']');
	}
}

Last updated

Copyright Ernest Marcinko