0

I have customised the standard Wordpress search.php and everything is working fine.

Now, my client would like to be able to add some content to the beginning of the search results page in the same way he can add content to any other page he has created in the admin area.

So, my thinking is to declare my custom search.php as a template with the name 'Search'. My client can then create a new page using the 'Search' template, add the content he needs and then display the search results.

How do I get the standard /?s=my+search+query to redirect to the new page AND deliver the information required to produce the results in my search.php?

I have tried this:

function fb_change_search_url_rewrite() {
    if ( is_search() && ! empty( $_GET['s'] ) ) {
        wp_redirect( home_url( "/custom-search-page/" ) . urlencode( get_query_var( 's' ) ) );
        exit();
    }   
}
add_action( 'template_redirect', 'fb_change_search_url_rewrite' );

and this:

function add_rewrite_rules_search_query($aRules) {
    $aNewRules = array('^search/(.+)?$' => 'index.php?pagename=custom-search-page&s=$matches[1]');
    $aRules = $aNewRules + $aRules;
return $aRules;
}
add_filter('rewrite_rules_array', 'add_rewrite_rules_search_query');

but no luck so far.

Thanks in advance for any help!

2 Answers 2

0

Maybe it´s easier to output some content from a different page/post in your search template which got a fixed slug.

I for example use this:

$page = get_posts(array('name' => 'welcome'));
if ($page)
{
   echo '<h1>'.$page[0]->post_title.'</h1>';
   echo '<p>'.$page[0]->post_content.'</p>';
}

This fetches the post with the slug "welcome" and displays its content. As I don´t use a single post display this works fine for my special case.

Maybe it can help you, too.

0

I would prefer to add a settings field like the page_for_posts option in the settings area by adding the following code somewhere in your functions.php. This will define the page_for_search option for you and easily get the value with get_option('page_for_search') function.

add_action('admin_init', 'my_custom_page_settings');
function my_custom_page_settings() {
  add_settings_section(
    'custom_page_settings',
    'Custom Page Settings',
    'custom_page_settings_helper_text',
    'reading'
  );

  add_settings_field(
    'page_for_search',
    'Page For Search',
    'page_for_search_callback',
    'reading',
    'custom_page_settings',
    array(
      'page_for_search'
    )
  );

  register_setting('reading','page_for_search', 'esc_attr');
}

function custom_page_settings_helper_text() {
  echo '<p>You can define default pages below.</p>';
}

function page_for_search_callback($args) {
  echo wp_dropdown_pages( array( 'name' => $args[0], 'echo' => 0, 'show_option_none' => __( '&mdash; Select &mdash;' ), 'option_none_value' => '0', 'selected' => get_option( 'page_for_search' ) ) );
}

Your client will be able to define a page for search in on Settings->Reading page and you can easily add your fields in your search.php template like this:

<?php $search_page_id = get_option('page_for_search'); ?>
<h1><?php echo get_the_title($search_page_id); ?></h1>
<div class="content"><?php echo get_post_field('post_content', $search_page_id); ?></div>

You can read more about the settings api here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.