Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have search a lot but cannot find any solution for my problem.

I when i entered the search term to the search box of my website, it goes to mysite.com/search/search+term. However, the result page does not have meta keywords and meta description. I trying to find way to add these values into the header of the result page with meta keywords = search term and meta description = information about the search term.

I think i will need to modify the search.php or query.php files, but not sure how to do. I don't want to change the header.php because it will conflict with other normal posts.

Anyone has any suggestion how to do this?

Thank you in advance!

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

that is relatively easy. you can add this to your theme's functions.php file

add_action('wp_head', 'add_meta_tags');
function add_meta_tags(){

  if(is_search()){
    $search_keyword = get_search_query();
    echo '<meta name="META_NAME" content="META TESTING" />';
  }
}

you may change echo '<meta name="META_NAME" content="META TESTING" />'; as you want to output the meta tags.

however, your theme needs to call wp_head(); for this to work. check your theme's header.php file.

share|improve this answer
    
Thank you very much, HungryCoder! It works almost perfectly. The only problem exists is the outcome is 2 meta value being continuous in a line, something like this <meta name="keyword" content="keyword"><meta name="description" content="description>. Is there any way to separate the 2 meta value in two line separately? Since i'm not sure if it affect SEO –  Pam Apple Jun 18 '12 at 15:57
    
That's very easy. echo '<meta name="META_NAME" content="META TESTING" />'.PHP_EOL;. however, both in same line is not a problem. –  HungryCoder Jun 18 '12 at 16:43
    
Okay, thank you very much for your great help –  Pam Apple Jun 18 '12 at 18:24
    
great. i think you can accept the answer. right? –  HungryCoder Jun 18 '12 at 18:59
add comment

Your specific solution depends on how your header.php file currently implements those two meta tags (static, filtered, etc.).

The simplest solution is to edit your header.php file but wrap your changes in the is_search() function:

<?php if ( is_search() ) : ?> . . . <?php else: ?> . . . <?php endif; ?>

The more graceful (/complicated) solution would be to filter the output with a custom filter.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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