Mini SEO – a Meta Description Field for Posts and Pages

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.

12 Replies to “Mini SEO – a Meta Description Field for Posts and Pages”

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

    1. 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.

      1. 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

  2. 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.

    1. 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.

  3. “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??

Leave a Reply to Wayan Adika Cancel reply

Your email address will not be published. Required fields are marked *