Automatically Create Meta Description From Content


Meta description tags are still very much used by search engines, they provide the default text to be displayed in the search results.

The Meta description tags should have relevant keywords to the article to attract visitors to your site.

In WordPress it can be done by using SEO plugins, but if you don't have these plugins installed or forgot to add the meta description to this plugin then here is the WordPress snippet for you.

This snippet will automatically take the first 125 characters of your content and add this into the meta description tag for the page.

function create_meta_desc() {
    global $post;
if (!is_single()) { return; }
    $meta = strip_tags($post->post_content);
    $meta = strip_shortcodes($meta);
    $meta = str_replace(array("\n", "\r", "\t"), ' ', $meta);
    $meta = substr($meta, 0, 125);
    echo "<meta name='description' content='$meta' />";
add_action('wp_head', 'create_meta_desc');
Back to top ^

Join Paulund

  • Get access to 100s of premium tutorials and downloadable content
  • Members content consists of Premium WordPress plugins, CSS packages, jQuery packages, tutorial demo files and templates for 100s of web development tutorials
  • In-depth development tutorials
  • Priority tutorial requests
  • Priority email development support
  • No ads

Here at Paulund you'll find Web Development tutorials and snippets focusing mainly on PHP, WordPress and CSS. Members will get access to premium tutorials that go into more detail about development with these languages. You will also get access to downloadable premium content based on the tutorial.

Join Paulund

Leave a Reply

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