Add Meta Descriptions WordPress

Home / Wordpress / Add Meta Descriptions WordPress

An extremely critical thing to give careful consideration to when enhancing your webpage for web crawler standing is making novel pages meta portrayals for the greater part of the pages on your site that you need to have filed. Assuming that you investigate Google, or any web search tool, you will see a blurb about your page underneath the page title. This small touch of content is taken for the content your page has in the meta depiction tag. No content, no blurb on Google, and perhaps no indexing whatsoever.

Having an extraordinary title for every page is paramount, however additionally essential is verifying that the depiction for your page is exactly as extraordinary and significant. The incredible thing about WordPress is that you can utilize some essential capacities to assemble a little script that might make a meta depiction rapidly for each of your pages.

I experienced a couple of issues attempting to assemble this script because of the way that a few plugins truly guide into the_content() or the_excerpt() and this makes a considerable measure of issues. The last result I thought of is underneath. Simply add the accompanying to your functions.php index inside the PHP tags:

function dynamic_meta_description() {
	$rawcontent = 	get_the_content();
	if(empty($rawcontent)) {
		$rawcontent = htmlentities(bloginfo('description'));
	} else {
		$rawcontent = apply_filters('the_content_rss', strip_tags($rawcontent));
		$rawcontent = preg_replace('/\[.+\]/','', $rawcontent);
		$chars = array("", "\n", "\r", "chr(13)",  "\t", "\0", "\x0B");
		$rawcontent = htmlentities(str_replace($chars, " ", $rawcontent));
	if (strlen($rawcontent) < 155) {
		echo $rawcontent;
	} else {
		$desc = substr($rawcontent,0,155);
		return $desc;

This little script grabs the raw content using get_the_content(), then it applies some filters and removes short codes, tags and line breaks. Once that is done, it limits the description to 155 characters if it is too long. Finally it returns the description, formatted exactly how you need it to be.

Next all we need to do is add a line to the header.php between the head tags.

<meta name="description" content="<?php echo dynamic_meta_description(); ?>" />

Assuming that a meta depiction line as of recently exists, you can swap it with this.

Right away the greater part of your pages will have an one of a kind meta depiction, and those that don’t will have your default site portrayal, which you set up in your wp-admin under your General Settings.

Related Posts

Leave a Reply

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