[Global Site Search] returning the global_site_search_form() in a function (in function.php)

Good day,
I am trying to integrate the Global Site Search plugin into my theme. I use a function to add the searchform into a wp_menu.

$menu_item = '<li class="right menusearch searchposition1">' . get_search_form( false ) . '</li>';

I would like to replace get_search_form( false ) with the according call that returns the code from global-site-search-form.php (if I choose to use global site search in the customizer). I have tried to use . global_site_search_form() . and get_template_part( 'global-site-search-form':wink: ... neither returned the expected form.
Does anyone have an idea on how to get this working?

In other parts of the theme I simply use
<?php if ( get_theme_mod( 'sicher_enable_multisite_search' ) == 1 ) : ?>
<?php global_site_search_form() ?>
<?php else: ?>
<?php get_search_form(); ?>
<?php endif; ?>
But for the search form in this wp_menu the form has to be in the functions.php.

Thanks for your help

  • Huberson

    Hello Stefan
    Instead of calling the functions directly inside the list tag, you can try creating two different functions to handles the form and adding it in menu.

    Basically, you'd create first function with 'global-site-search-form.php' content and return the form. Then include it in the menu using a filter.

    Something like this:

    function global_search_custom_form() {
    	global $current_blog;
    
    	$form ='<form action='. esc_url( trailingslashit( $current_blog->path . global_site_search_get_search_base() ) ) . '>
    		<table border="0" cellpadding="2" cellspacing="2" style="width:100%">
    			<tr>
    				<td style="width:80%">
    					<input type="text" name="phrase" style="width:100%" value="' . esc_attr( stripslashes( global_site_search_get_phrase() ) ) . '">
    				</td>
    				<td style="text-align:right;width:20%">
    					<input type="submit" value="' . __( 'Search', 'globalsitesearch' ) . '">
    				</td>
    			</tr>
    		</table>
    	</form>';
    
    	return $form;
    }
    
    function add_search_box_to_menu( $items, $args ) {
        if( $args->theme_location == 'primary' )
            return $items . "<li class='menu-header-search'>" . global_search_custom_form() . "</li>";
    
        return $items;
    }
    add_filter('wp_nav_menu_items','add_search_box_to_menu', 10, 2);

    Cheers,
    Huberson

  • Stefan

    So, I was able to find a solution. And funny enough, it was by simplifying instead of complicating. :slight_smile:

    Since the searchform() and global_site_search_form() are supposed to look identical, I added the <?php if ( get_theme_mod( 'sicher_enable_multisite_search' ) == 1 ) : ?> directly into the searchform.php. So when the multisite search is enabled, the action of the form field is set to <?php echo esc_url( trailingslashit( $current_blog->path . global_site_search_get_search_base() ) ) ?> and the name of the search input is set to name="phrase". That's all.

    Now my global-site-search-form.php simply loads <?php get_template_part( 'searchform'); ?> and I can use the same function as before, with $menu_item = '<li class="right menusearch searchposition1">' . get_search_form( false ) . '';

    Cheers
    Stefan

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.