49 | Mini SEO – a Meta Description Field for Posts and Pages

g+

Modern search engines do not rely on meta descriptions and meta keywords anymore for your ranking.

However, they do preferably show a proper added meta description instead of the first few words of the post or page.

If you can enter this meta description into your WordPress blog or CMS, specifically for each post and static page, you can fine tune what you would like your customers to see with the search results.

Most users would download and install one of the many SEO plugins, which often come with a lot of unwanted features.

With a bit of programming, you can add your own ‘Meta Description Field’ below the post/page editor.

The first part of the code needs to be added into functions.php of your theme:

// 'Custom Meta Description' field below post/page editor
add_action('admin_menu', 'custom_meta_desc');
add_action('save_post', 'save_custom_meta_desc');
function custom_meta_desc() {
add_meta_box('custom_meta_desc', 'Add meta description <small>(if left empty, the first 200 characters of the excerpt will be used)</small>', 'custom_meta_desc_input_function', 'post', 'normal', 'high');
add_meta_box('custom_meta_desc', 'Add meta description <small>(if left empty, the first 200 characters of the excerpt will be used)</small>', 'custom_meta_desc_input_function', 'page', 'normal', 'high');
}
function custom_meta_desc_input_function() {
global $post;
echo '<input type="hidden" name="custom_meta_desc_input_hidden" id="custom_meta_desc_input_hidden" value="'.wp_create_nonce('custom-meta-desc-nonce').'" />';
echo '<input type="text" name="custom_meta_desc_input" id="custom_meta_desc_input" style="width:100%;" value="'.get_post_meta($post->ID,'_custom_meta_desc',true).'" />';
}
function save_custom_meta_desc($post_id) {
if (!wp_verify_nonce($_POST['custom_meta_desc_input_hidden'], 'custom-meta-desc-nonce')) return $post_id;
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return $post_id;
$customMetaDesc = $_POST['custom_meta_desc_input'];
update_post_meta($post_id, '_custom_meta_desc', $customMetaDesc);
}

The second part will be added into header.php, below the <title> tag:

<meta name="description" content="<?php
if( is_single() || is_page() ) :
$text = get_post_meta($post->ID,'_custom_meta_desc',true);
if(!$text) $text = ($post->post_excerpt) ? $post->post_excerpt : substr($post->post_content, 0, 200).'...';
echo esc_attr(strip_tags(apply_filters('get_the_excerpt',$text)));
else :
/* optional area to program meta descriptions for index and archive pages, etc */
endif; ?>" />

meat description input field

'Meta Description Input' for posts and pages

The new input field will show directly below the post or page editor window.

Simply add your meta description text into the field, and press the usual ‘update’ button;
if left empty, the meta description will show the first 200 characters of the post or page.

This entry was posted in Tips & Tricks, WordPress and tagged , , , , . Bookmark the permalink.

14 Responses to Mini SEO – a Meta Description Field for Posts and Pages

  1. Buford comments:

    Free new 12 months to forex duality review the Choices traders.

  2. Chase comments:

    This time, with adtrics academy review academy it’s 10x better.

  3. Wayan Adika comments:

    Thanks, for the tutorial
    but how do I put meta keywords ?
    you have any trick, do you mind to shared

    • michael replies:

      the same principle applies; you need the replace any references to ‘meta-desription’ with reference to ‘meta-keywords’ and possibly use the post categories and post tags as default values.

      • Wayan Adika replies:

        Dear Michael, thanks for reply
        I new in php script, is it your mean just copy all code and replace the “meta-description” with “meta-keywords” and will show on excerpt for manual insert the keywords.
        I know if Google doesn’t use meta keyword for web search ranking, but other search engine still using it. So i would like to put in my website

      • michael replies:

        instead of programming your own, have you considered to use one of the seo plugins for the meta keywords? https://wordpress.org/plugins/search.php?q=meta+keywords+seo

  4. Amin comments:

    It Works, Thank you for this great work

  5. Shoeb Siddique comments:

    I have tried this script it’s working great. I am here to ask for help, can you also provide me the code through which i can add meta keyword below the meta description.

    Thank You.

    • michael replies:

      it involves basically creating a copy of the code, and exchanging any strings, i.e. for example change description to keywords and desc to keyw;
      also, I would change the automatic use of the excerpt for instance to getting a list of the post’s categories and tags.
      If you need help, please contact me via email; I can provide the exact code for a small fee.

  6. kochi comments:

    thank … greaaatttt

  7. Shakir comments:

    After a long search in google, i found your tip is appropriate for me. Thanks.

  8. Pete comments:

    “Modern search engines do not rely on meta descriptions and meta keywords anymore for your ranking.”

    Are you sure about meta descriptions? Whenever I do a Google search with some choice keywords the meta descriptions are shown in the Google results and are highlighted (bolded). It looks like Google loves having those keywords in the meta description whenever I search??

  9. Fatih Toprak comments:

    great tips 😉 Thanks.

  10. Pingback: 9 Mini Seo Sites | vptoms WordPress

    g+

    21/01/2012 | […] Mini SEO a Meta Description Field for Posts and Pages .Jan 3, 2012 Powerful Tools Mini SEO a Meta…

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.