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.
1
add_action('wp_footer', 'asp_insert_sc_to_footer', 99999);
2
function asp_insert_sc_to_footer() {
3
// Replace this with the search ID you want to use
4
$id = 1;
5
// Comma separated list of Pages (or any CPT) where the search should be excluded. Leave it empty to ignore.
6
$exclude_on_pages = '1, 2, 3';
7
// Comma separated list of Pages (or any CPT) where the search should be excluded. Leave it empty to ignore.
8
$include_on_pages = '';
9
// Exclude on archive pages?
10
$exclude_on_archives = false;
11
// Exclude on category (or any taxonomy) archive pages. Comma separated list of taxonomies.
12
$exclude_on_tax_archives = 'category, tag';
13
// Should it be visible on the front page? true or false
14
$exclude_on_front = false;
15
16
// -------- DO NOT TOUCH BELOW ----------
17
if ( is_front_page() && $exclude_on_front )
18
return false;
19
20
if ( is_archive() && $exclude_on_archives )
21
return false;
22
23
$eta = array_filter( explode(",", str_replace(' ', '', $exclude_on_tax_archives)), 'strlen' );
24
foreach ( $eta as $_eta ) {
25
if ( ($_eta == 'tag' || $_eta == 'post_tag') && is_tag() )
26
return false;
27
if ( $_eta == 'category' && is_category() )
28
return false;
29
if ( is_tax($_eta) )
30
return false;
31
}
32
33
$epa = array_filter( explode(",", str_replace(' ', '', $exclude_on_pages)), 'strlen' );
34
$ipa = array_filter( explode(",", str_replace(' ', '', $include_on_pages)), 'strlen' );
35
$pid = get_the_ID();
36
if ( !is_wp_error($pid) ) {
37
if ( in_array($pid, $ipa) || !in_array($pid, $epa) ) {
38
echo do_shortcode('[wd_asp id='.$id.']');
39
}
40
} else {
41
echo do_shortcode('[wd_asp id='.$id.']');
42
}
43
}
Copied!
Last modified 1yr ago