SEO Meta description write panel

The most significant factors for WordPress SEO of your blog article:

  • the page title
  • the page url
  • the page meta description
  • the page quality
  • the domain page rank

Here I will provide the code to create meta description custom write panel that will reside inside your post and page editing screen.

Now, there are different solutions for this specific problem, there are excellent WordPress plugins such as Simple Meta Description that will do exactly this:

function tlf_add_meta_description() {
	if (is_single()) {
		$description = strip_tags(get_the_excerpt());
		$description = preg_replace('/\s\s+/', ' ', $description);
		echo '';
	}
}
add_action('wp_head','tlf_add_meta_description')

…, but these are using the excerpt field to add a meta description to posts. Sometimes the excerpt field is needed as is, and the meta description is needed as separate field. I solved the problem using so called custom write panel that can be achieved via: add_meta_box and update_post_meta WordPress functions.

/*smd = seo meta description*/
add_action( 'add_meta_boxes', 'smd_add_custom_box' );
add_action( 'save_post', 'smd_save_postdata' );

/*post and page are having custom edit panel for Meta Description*/
function smd_add_custom_box() {
    add_meta_box(
        'myplugin_sectionid',
        __( 'Meta', 'seo_textdomain' ),
        'smd_inner_custom_box',
        'post'
    );
    add_meta_box(
        'myplugin_sectionid',
        __( 'Meta', 'seo_textdomain' ),
        'smd_inner_custom_box',
        'page'
    );
}

/* Prints the box content */
function smd_inner_custom_box() {
  // Use nonce for verification
  global $post;
  wp_nonce_field( plugin_basename( __FILE__ ), 'myplugin_noncename' );
  $meta_box_value = get_post_meta($post->ID, 'seometadescription', true);

  // The actual fields for data entry
  echo '<label for="seometadescriptoin">';
       _e("Meta Description", 'seo_textdomain' );
  echo '</label>';
  echo '<textarea id="seometadescription" style="width: 100%;" name="seometadescription">';
  echo $meta_box_value ;
  echo '</textarea>';
   _e("only 139 chars visible on Google results", 'seo_textdomain');
}
function smd_save_postdata( $post_id ) {
  // verify if this is an auto save routine.
  // If it is our form has not been submitted, so we dont want to do anything
  if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
      return;
  // verify this came from the our screen and with proper authorization,
  // because save_post can be triggered at other times
  if ( !wp_verify_nonce( $_POST['myplugin_noncename'], plugin_basename( __FILE__ ) ) )
      return;
  // Check permissions
  if ( 'page' == $_POST['post_type'] )
  {
    if ( !current_user_can( 'edit_page', $post_id ) )
        return;
  }
  else
  {
    if ( !current_user_can( 'edit_post', $post_id ) )
        return;
  }
  // OK, we're authenticated: we need to find and save the data
  $mydata = strip_tags($_POST['seometadescription']);
  $mydata = preg_replace('/\s\s+/', ' ', $mydata);
  // save/update
  update_post_meta($post_id, 'seometadescription', $mydata);
  return $mydata;
}

Once we have this code we will need to update the header for our posts either through header.php, or to create a hook for wp_head function like this:

function add_meta_description() {
  global $post;
  $description = get_post_meta($post->ID, 'seometadescription', true);
  $description = preg_replace('/\s\s+/', ' ', $description);
  echo '';
}
add_action('wp_head','add_meta_description');

tags: & category: -