Wednesday, 26 February 2014

Automatically create meta description from the_content

  1. Adding this snippet into the functions.php file of your WordPress theme will automatically create a meta description from your WordPress post, striping out all shortcodes and tags. Also ensure that you have it in the header.php of your WordPress theme or this snippet will not function.
  2.  
  3. function create_meta_desc() {  
  4.     global $post;  
  5.     if (!is_single()) { return; }  
  6.     $meta = strip_tags($post->post_content);  
  7.     $meta = strip_shortcodes($post->post_content);  
  8.     $meta = str_replace(array("\n", "\r", "\t"), ' ', $meta);  
  9.     $meta = substr($meta, 0, 125);  
  10.     echo "<meta name='description' content='$meta' />";  
  11. }  
  12. add_action('wp_head', 'create_meta_desc'); 

post first

this is first post