0

Currently, the search bar is a widget in the primary sidebar.

I want to remove it from all pages except those using a specific page template (let's call that template "searchable.php").

My most recent attempt, which sits in an mu-plugins file:

function nwb_remove_search_widget() {
    global $post;
    if ( get_page_template_slug( $post->ID ) == 'searchable.php' ) return;
    unregister_widget('WP_Widget_Search');
}
add_action( 'widgets_init', 'nwb_remove_search_widget' );

This doesn't work. I think the problem may be that the page template hasn't been established at 'widgets_init', so the condition test returns false.

I have also tried 'is_page_template('searchable.php') to no avail.

I have also tried this...

add_action( 'the_post', 'nwb_remove_search_widget' );

...to no avail.

Thanks in advance for any help anyone might send my way.

  • Might be easier to create a second sidebar. That way you just include the "no search" sidebar everywhere except the template where you want it, and include the "with search" sidebar there. – WebElaine Oct 23 '18 at 13:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.