Support » Developing with WordPress » Custom search form in theme

  • Resolved Guido

    (@guido07111975)


    Hi there,

    My theme has a custom search form, for posts only. The structure is something like the first example here.

    But the Theme Check plugin generates a warning:
    WARNING: role=”search” was found in header.php. Use get_search_form() instead of hard coding forms. Otherwise, the form can not be filtered.

    So I’ve changed the search form into this:

    
    <?php $native_search_form = get_search_form( false );
    $custom_search_form = str_replace( '<input type="submit"', '<input type="hidden" name="post_type" value="post"><input type="submit"', $native_search_form );
    echo $custom_search_form; ?>
    

    Is this a proper way to hook into the default search form?

    Guido

Viewing 5 replies - 1 through 5 (of 5 total)
  • If your theme is for the WP repository, you should not be limiting the search to posts. That is plugin territory.
    If your theme is for your own use, do whatever you want.

    The easiest way for a theme to change the search form is to put the HTML into searchform.php and use get_search_form() so that child themes and plugins can filter it.

    Thread Starter Guido

    (@guido07111975)

    Hi Joy,

    Theme is listed here.. It’s for creating a knowledge base and this custom search for posts is an important element of this theme.

    When creating a searchform.php this file will be used for every search form. Not an option. That’s why I added the hardcoded form.

    Regarding this part: Otherwise, the form can not be filtered.
    What does this mean, that users cannot hook into this search? In this case the search form is added for posts only, so no problem… I guess?

    Guido

    “creating a knowledge base and custom search” is plugin territory. The theme should not have code specific to this, because when a user switches themes, then that functionality is lost.
    The theme provides the layout of the HTML on the page, in a way that child themes and plugins can filter it to be different. So, don’t hard-code the search form and don’t limit the search to posts if the theme is in the WP repository.

    Thread Starter Guido

    (@guido07111975)

    Got it, thank you. Cannot simply remove feature from this theme. So have to look for an alternative solution.

    Guido

    Hello @guido07111975

    Please mention me in your theme ticket. I will look there.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Custom search form in theme’ is closed to new replies.