Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Tips Auto Generate Meta Description Without Plugins In Wordpress
#1
Meta description is an important factor for Search Engine Optimization (SEO). Search engines use it for display snippet in the search results. Via meta description, visitors can see a brief and concise summary of your page’s content. In WordPress, if you use SEO plugins like All-In-One SEO, it already has the built-in feature for auto generation meta description tag. But you can do it yourself without any plugins, and here it is!

The idea of generating meta description is getting the first paragraph of your post and remove all formats, truncate it to a appropriated length so search engines can understand and display it correctly.

To do that, simply paste the following code into your functions.php file of your theme:

Code:
add_action( 'wp_head', 'prefix_meta_desc' );
function prefix_meta_desc() {
    if ( ! is_singular() )
        return;

    $meta = strip_tags( get_post()->post_content );
    $meta = str_replace( array( "\\n", "\\r", "\\t" ), ' ', $meta);
    $meta = substr( $meta, 0, 155 );

    echo "<meta name='description' content='$meta'>";
}

The code above will run only in singular pages (pages, single posts and all single custom post types). It removes all HTML tags from the post content, replaces all special whitespace characters (tabs, new lines) to a single space and truncates it to 155 characters.

Be sure that you added wp_head() in your theme header.php file.

Why should meta description have 155 characters?
According to the study SEO, the appropriate length of meta description is around 155 characters. The search engines don’t recommend an exact number because it depends on which language of your website and the length of the words.

Meta descriptions can be any length, but search engines generally truncate snippets longer than 160 characters. It is best to keep meta descriptions between 150 and 160 characters.

Now you can forget the SEO plugins and makes your theme optimized for SEO. If you find anything interesting about SEO or meta description tag, please let me know in the comments.
  


Messages In This Thread
Auto Generate Meta Description Without Plugins In Wordpress - by lagosmp - 02-05-2019, 11:36 PM

Possibly Related Threads...
Thread Author Replies Views Last Post
  SHARED Top Entertainment Keyword To Rank For (November Edition) Adegoldop6 0 4 7 hours ago
Last Post: Adegoldop6
  Google Talks About Building Too Many Links Too Fast Kingsolomon 1 93 11-08-2019, 09:07 PM
Last Post: Iamkimlarry
  How to Optimize Your PDF Files to Get a Featured Snippet 2019 Kingsolomon 0 225 08-04-2019, 07:04 AM
Last Post: Kingsolomon
  Terms you must know about keywords Bloggersng 0 198 07-15-2019, 04:22 PM
Last Post: Bloggersng
  Five ways in which Twitter can help with link building Kingsolomon 2 553 07-05-2019, 06:38 AM
Last Post: Bloggersng

Forum Jump:


Users browsing this thread:
1 Guest(s)

Auto Generate Meta Description Without Plugins In Wordpress00