Site logo

Archived topic

Menu Search

5 replies · Started by Ashley on May 13, 2019

Viewing posts 1–6 of 6

Hi Guys,

I'm wanting to utilize the search in the navigation feature and want it to only search for woocommerce products.

I've used the below to move the search to the secondary menu and then followed this guide to utilise the WooCommerce search and add a placeholder.

Now, however, I'm getting duplicate search field appears when clicking on search and hitting 'enter' no longer does anything.

// Move Nav search to secondary menu
add_filter( 'wp_nav_menu_items', 'tu_secondary_menu_search_icon', 10, 2 );
function tu_secondary_menu_search_icon( $nav, $args ) {
$generate_settings = wp_parse_args(
get_option( 'generate_settings', array() ),
generate_get_defaults()
);

if ( 'enable' !== $generate_settings['nav_search'] ) {
return $nav;
}

if ( $args->theme_location == 'secondary' ) {
return $nav . '<li class="search-item" title="' . esc_attr_x( 'Search', 'submit button', 'generatepress' ) . '"><span class="screen-reader-text">' . _x( 'Search', 'submit button', 'generatepress' ) . '</span>

';
}

return $nav;
}

// Search For WooCommerce Products
add_filter( 'generate_navigation_search_output', function() {
printf(
'<form method="get" class="search-form navigation-search" action="%1$s">
<input type="search" class="search-field" value="%2$s" name="s" title="%3$s" />
<input type="search" placeholder="Search for a course and hit enter..." class="search-field" value="%2$s" name="s" title="%3$s" />
</form>',
esc_url( home_url( '/' ) ),
esc_attr( get_search_query() ),
esc_attr_x( 'Search', 'label', 'generatepress' )
);
} );

Hi there,

can you remove the second filter function that you're using to change the text and search option and let us know if there is only one working search element in place?

Hi David,

That seems to have removed the secondary search field and reinstated the functionality.

I'm still looking to have a palceholder and to only search WooCommerce products if you're able to help with that at all?

Hi Leo,

If you refer to my original post you'll see this is what I've tried to use already.

Weird. I just tried this snippet which takes care of moving the search to secondary navigation, only search for product and add a placeholder without any issues:

add_action( 'after_setup_theme', 'tu_move_nav_search' );
function tu_move_nav_search() {
	remove_action( 'generate_inside_navigation', 'generate_navigation_search' );
	remove_action( 'generate_inside_navigation', 'generate_mobile_menu_search_icon' );
	
	add_action( 'generate_inside_secondary_navigation', 'generate_navigation_search' );
	add_action( 'generate_inside_secondary_navigation', 'generate_mobile_menu_search_icon' );
	
	remove_filter( 'wp_nav_menu_items', 'generate_menu_search_icon', 10, 2 );
}

add_filter( 'wp_nav_menu_items', 'tu_secondary_menu_search_icon', 10, 2 );
function tu_secondary_menu_search_icon( $nav, $args ) {
	$generate_settings = wp_parse_args(
		get_option( 'generate_settings', array() ),
		generate_get_defaults()
	);

	if ( 'enable' !== $generate_settings['nav_search'] ) {
		return $nav;
	}

	if ( $args->theme_location == 'secondary' ) {
		return $nav . '<li class="search-item" title="' . esc_attr_x( 'Search', 'submit button', 'generatepress' ) . '"><a href="#"><span class="screen-reader-text">' . _x( 'Search', 'submit button', 'generatepress' ) . '</span></a></li>';
	}

	return $nav;
}
add_filter( 'generate_navigation_search_output', function() {
    printf(  
        '<form method="get" class="search-form navigation-search" action="%1$s">
            <input type="search" placeholder="Enter your search" class="search-field" value="%2$s" name="s" title="%3$s" />
            <input type="hidden" name="post_type" value="product" />
        </form>', 
        esc_url( home_url( '/' ) ), 
        esc_attr( get_search_query() ),   
        esc_attr_x( 'Search', 'label', 'generatepress' ) 
    ); 
} );

Can you give it another shot?

This archived topic is closed to new replies.