Site logo

Archived topic

Add Search to anywhere on page!

9 replies · Started by Tom on September 26, 2022

Viewing posts 1–10 of 10

Hi all,

I've looked around the forum for the answer to this but I can't find anything (I may of missed it though!)

I would like to add search to a top bar I've created with GP and GB, but the GP search function seems to be only available if you have a navigation menu set up. The top bar I've created doesn't have any navigation menus in it. It consists of social media icons and a phone number. I wanted to add a search function as well to the top bar.

I'm sorry if this has already been answered before!
Many Thanks

Hi Sy,

One approach is to go to Appearance > Widgets > Top Bar, and add a WordPress Search Block there. You may also do this in Appearance > Customize > Widgets > Top bar.

Thank you for the super quick reply! Sorry, I should of mentioned, I've created the top bar in Elements > Block > Hook > generate_before_header. Would that still work?

Thanks

In that case, you can add the search block into your generate_before_header hook element.

Thank you for your reply Ying, But, I mean the GP search magnifying glass not the WP native search block as it doesn't look that good!

Thanks!

Add this PHP snippet:

function portable_func($atts, $content = null) {
      ob_start();
      do_action('portable_hook');
      return ob_get_clean();
}
add_shortcode('portable-hook-shortcode', 'portable_func');

add_action( 'wp', function() {
    if ( 'enable' === generate_get_option( 'nav_search' ) ) {
        remove_action( 'generate_menu_bar_items', 'generate_do_navigation_search_button' );
        add_action( 'portable_hook', 'generate_do_navigation_search_button' );

        remove_action( 'generate_inside_navigation', 'generate_navigation_search' );
        add_action( 'portable_hook', 'generate_navigation_search' );
    }
}, 20 );

Adding PHP: https://docs.generatepress.com/article/adding-php/#code-snippets

Then add this Shortcode in your Element through a Shortcode Block - [portable-hook-shortcode]

Thank you for providing the code. I added the PHP to Code Snippets and activated it then added a Shortcode block to the page but nothing appears, have I missed something out somewhere?

Thanks

Can you make sure the navigation search is enabled at customizer > layout > primary navigation?

Excellent! Thank you

No problem :)

This archived topic is closed to new replies.