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 run a multiple author platform where I have added conditions that force authors to add tags to their post. To improve SEO, I would like to add Meta Tags to the posts. Ideally, with a light solution (have high traffic) through functions or custom plugin.

There are two meta tags that I want to add, first one is description:

<meta name="description" content="Description should be no more than 150 characters" />

For the description, I want to grab the excerpt of the post and strip it to 150 character max (including spaces). If the post doesn't have an excerpt, then grab the first 150 characters of the body text (excluding shortcodes if there are any).

For the keywords:

<meta name="keywords" content="Keyword, Keyword 2, Keyword 3" />

Each keyword or keyword phrase need to be separated by a comma and then a space. I want to grab the category and tags of the post and add it here. For instance, if my post's category is Movie, and the tags are Oldboy, Revenge and South Korean - then the meta keywords would be: content="Movie, Oldboy, Revenge, South Korean"

Also, obviously, the code need to be executed only on Post pages. And I am guessing that the <head> HTML tag comes far before the actual loop, so I'll need something else than the global $post.

If you choose to answer, please be detailed and add comments in your code so that I can understand and learn from you.

share|improve this question

1 Answer 1

up vote 3 down vote accepted

I would keep out of your header.php & either add the following to your functions.php or wrap up as a plugin:

add_action( 'wp_head', 'wpse_71766_seo' );

/**
 * Add meta description & keywords for single posts.
 */
function wpse_71766_seo()
{
    if ( is_single() && $post_id = get_queried_object_id() ) {

        if ( ! $description = get_post_field( 'post_excerpt', $post_id ) )
            $description = get_post_field( 'post_content', $post_id );

        $description = trim( wp_strip_all_tags( $description, true ) );
        $description = substr( $description, 0, 150 );

        $keywords = array();    
        if ( $categories = get_the_category( $post_id ) ) {
            foreach ( $categories as $category )
                $keywords[] = $category->name;
        }

        if ( $tags = get_the_tags( $post_id ) ) {
            foreach ( $tags as $tag )
                $keywords[] = $tag->name;
        }

        if ( $description )
            printf( '<meta name="description" content="%s" />' . "\n\t", esc_attr( $description ) );
        if ( $keywords )
            printf( '<meta name="keywords" content="%s" />' . "\n\t", esc_attr( implode( ', ', $keywords ) ) );

    }   
}

This hooks onto the wp_head action & outputs the required meta only if currently viewing a single post.

Edit: Fixed two equal symbols that were missing.

share|improve this answer
1  
Thank you for the answer, however there are a few issues with your code. Firstly, both the description and keyword has a typo for the content, it is missing an equal symbol, it prints like this: <meta name="keywords" conten**t"c**ategory, " />. Secondly, the tags were not included in the keywords, why? Is there any chance that you could kindly check through your code example for these two issues? –  Amanda Duke Nov 7 '12 at 0:10
    
I went ahead and fixed the typo for the equal symbols, however, I cannot find why the tags are not being added to the keywords, do you know why? –  Amanda Duke Nov 7 '12 at 0:14
2  
Another typo: $keywords[] = $tags->name; (fixed). In all fairness I did write this on the fly ;) –  TheDeadMedic Nov 7 '12 at 9:53
    
Don't be silly, when I was posting this, I was expecting some guidelines, not a full code that you provided, cheers! To perfect the code, I have a final request. It appears that if there is a new line in the test/excerpt, that new line is added to the description as well. This is a problem as many of my articles starts with a heading (and then a new line to the paragraph). Is there any way to alter the code so that it skips new lines? –  Amanda Duke Nov 7 '12 at 15:27
    
Replace strip_tags( $description ) with wp_strip_all_tags( $description, true ) - TBH I should've used that from the get-go. –  TheDeadMedic Nov 7 '12 at 15:30

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.