[Resolved] Search Field in 404 Page

Home Forums Support [Resolved] Search Field in 404 Page

Home Forums Support Search Field in 404 Page

Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #786913
    Marius

    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?

    #787184
    Tom
    Lead Developer
    Lead Developer

    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]

    #787199
    Marius

    Okay, but where to put this code? I have searches for it and find this: https://docs.generatepress.com/article/creating-a-shortcode/

    But it doesn´t say, where to put the code.

    #787200
    Tom
    Lead Developer
    Lead Developer

    Sorry about that 🙂

    You can add it using one of these methods: https://docs.generatepress.com/article/adding-php/

    #787215
    Marius

    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

    #787223
    Tom
    Lead Developer
    Lead Developer

    Sorry about that – I just adjusted the code above: https://generatepress.com/forums/topic/search-field-in-404-page/#post-787184

    Can you give it another shot?

    #787236
    Marius

    okay, now it workes. Thank you Tom.

    #787439
    Tom
    Lead Developer
    Lead Developer

    You’re welcome 🙂

    #1159963
    Dana

    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.

    #1159973
    Dana

    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();
    } );
    #1160027
    Leo
    Staff
    Customer Support

    Glad you’ve figured out 🙂

Viewing 11 posts - 1 through 11 (of 11 total)
  • You must be logged in to reply to this topic.