Site logo

Archived topic

Search Field in 404 Page

10 replies · Started by Marius on January 20, 2019

Viewing posts 1–11 of 11

With the help from the plugin 404page, I want to make a separate 404 page.

But how can I put the search field in there, like in the standard 404 page from GP?

Hi there,

You could create a shortcode which will output the form for you:

add_shortcode( 'search_form', function() {
    ob_start();

    get_search_form();

    return ob_get_clean();
} );

Then you could use this on your page: [search_form]

Hmm, I have done so, but when openig this site, I get this error:

Warning: Cannot modify header information - headers already sent by (output started at /www/htdocs/xxx/domain.net/wp-includes/general-template.php:241) in /www/htdocs/xxx/domain.net/wp-includes/pluggable.php on line 1223

okay, now it workes. Thank you Tom.

You're welcome :)

Hello GP support team,

If you would like me to start a new topic I can. I just figured this is relevant to the current topic.

I have used the search_form shortcode above to add to a couple of Elements; one for my 404 page and one for my search page.

I also used the filter suggestion and code to exclude pages from my search and only focus on posts being returned in the search results and it works from the nav search.

The filter suggestion is not working for my search_form shortcode as searching from either my 404 or search pages where the search_form shortcode is declared returns pages in the results.

Is there a way to have the same filter applied to the search_form shortcode? Or is the filter only specific to the navigation bar search? I took a peek at the other filters and I didn't see another filter that I could use.

Thanks in advance.

Hi GP Team,

You can disregard my question. I figured it out.

Thanks.

I thought I would post the code I used in case anyone else wants to do the same.

add_shortcode( 'search_form', function() {
    ob_start();

    printf(  
        '<form method="get" class="search-form" action="%1$s">
             <input type="search" placeholder="Search mysite.com" class="search-field" value="%2$s" name="s" title="%3$s" />
            <input type="submit" value="Search">
            <input type="hidden" name="post_type" value="post" />
        </form>', 
        esc_url( home_url( '/' ) ), 
        esc_attr( get_search_query() ),   
        esc_attr_x( 'Search', 'label', 'generatepress' ) 
    );
	
    return ob_get_clean();
} );

Glad you've figured out :)

This archived topic is closed to new replies.