Site logo

Archived topic

Trying to insert navigation search in top bar (instead of in navigation)

3 replies · Started by Lars on March 1, 2023

Viewing posts 1–4 of 4

Hi!

Using this to create navigation search short code:

function generate_navigation_search_shortcode() {
  // Call the theme's built-in function
  $navigaion_button = generate_do_navigation_search_button(); 
  $search_form = generate_navigation_search();
  $output = $navigaion_button .$search_form;

   echo $output;
 
}
add_shortcode( 'gp_navigation_search', 'generate_navigation_search_shortcode' );

Added this inside of a container of generateblocks in a custom created top bar element hook. Web page: https://nyatidensmedicin.se

It’s adding itself outside of the container for some reason. How can I make it be added inside of the top bar container?

Hi there,

shortcodes should return their data and not echo them.
However those functions will need to printed to the screen, so in that case you can use object buffering.
Change the shortcode to:

function generate_navigation_search_shortcode() {
  // start object buffer
  ob_start();
  // Call the theme's built-in function
  $navigaion_button = generate_do_navigation_search_button(); 
  $search_form = generate_navigation_search();
  $output = $navigaion_button .$search_form;
  echo $output;
  // return buffer
  return ob_get_clean();
}
add_shortcode( 'gp_navigation_search', 'generate_navigation_search_shortcode' );

I am not exactly sure how this code made it work, but it did! I hope to level my PHP-game by the end of the year, or the next one!

I am using an overlay search with another code you have been giving me before in another earlier thread: https://generatepress.com/forums/topic/moving-search-field-and-icon/#post-1540468 but it turns out a bit funny now, this particular time.

You can see the results here:
Shared Image

Or directly through the website (if you click the search icon in the upper right corner):
https://nyatidensmedicin.se

There seem to be some kind of duplication happening and I can’t locate it in the source.

Ah, its because the original search form is still being output.
Instead make your shortcode just output the button.

function generate_navigation_search_shortcode() {
  // start object buffer
  ob_start();
  // Call the theme's built-in function
  $navigation_button = generate_do_navigation_search_button(); 
  echo $navigation_button;
  // return buffer
  return ob_get_clean();
}
add_shortcode( 'gp_navigation_search', 'generate_navigation_search_shortcode' );

And then style the original form.

This archived topic is closed to new replies.