I built a custom search page with a form like:
<form role="search" method="get" id="searchform" action="<?php echo home_url( '/' ); ?>">
trouble is I want the results to appear in search.php
whereas I m led to index.php
How can I force to always go to search.php ?
I built a custom search page with a form like:
<form role="search" method="get" id="searchform" action="<?php echo home_url( '/' ); ?>">
trouble is I want the results to appear in search.php
whereas I m led to index.php
How can I force to always go to search.php ?
Share Improve this question asked Sep 22, 2012 at 3:57 p.a.p.a. 5813 gold badges13 silver badges29 bronze badges3 Answers
Reset to default 0If I get it correctly Wordpress loads the search template dynamically by the parameters delivered to it. Specifically the s
parameter. Meaning if you try to send the user to the http://www.example/search.php
you'll get 404 error page.
For now the only workaround I can think is to create a function which is hooked to the init
action hook that listens for a parameter in order to load the right file. The function must reside in the functions.php
of your theme (assuming you are developing a theme). The function goes like this:
function load_custom_search_template(){
if(isset($_REQUEST['custom_search'])){
require('my_search.php');
die();
}
}
add_action('init','load_custom_search_template');
Template tag for the rescue
Simply use get_search_form( $echo )
for the search form.
This template tag first searches for a searchform.php
file in your main theme folder. If there is no such template, then you'll get a default search form. And depending on your input argument, you can echo or just return it for later use. It also has a filter on its end, that contains the complete search form HTML as string: get_search_form
with one argument: $form_html
.
How and what to use for search form modifications
So if you want to alter the output, just append, prefix, replace whatever...
- use the
get_search_form
-filter to pre-/append something or completely replace the form - use the custom template to build a unique search form
Or get really sophisticated with theme structure and use the filter to load your searchform from somewhere in your nested theme structure:
// In your functions.php
function wpse65934_searchform_location( $form )
{
return get_template_part( 'subfolder/searchform', 'default' );
}
add_filter( 'get_search_form', 'wpse65934_searchform_location' );
Try adding a hidden input of keyword search, as it'll redirect to the search.php template
<input type="hidden" name="s" id="search" value="<?php the_search_query(); ?>" />