How to add meta description in WordPress without plugin

add wordpress meta description without plugin

Here’s how to add meta description in WordPress without plugin for homepage and single post. By adding a code in functions.php file, the meta description will be generated in the head section immediately when you publish your post.

Adding meta description automatically in WordPress through a plugin is easy. But the use of plugins to build the entire site structure is not recommended. So if you do not use many SEO features you can add meta description in WordPress without plugin.

Below we will specify 3 php codes, one for the homepage, one for single post and another for both home page and single post. Third code is to use as a single code for both templates.

 

To add the meta description for homepage only, use this code in  functions.php in your child theme:

function add_custom_meta_des(){
if( is_home() || is_front_page() ){
$meta_des = "Your website description here";
echo '<meta name="description" content="' . $meta_des . '" />'; } }
add_action( 'wp_head', 'add_custom_meta_des', 4 );

Replace this “Your website description here” with your own website description.

To generate the meta description for single posts only, use this code in  functions.php in your child theme:

This part of the code for Single Posts has been updated on Appril 05 2019 due to an error identified in GitHub.

function create_meta_desc() {
 global $post;
if (!is_singular()) {return; }
elseif(!empty( $post->post_excerpt)) {
echo "<meta name='description' content='$post->post_excerpt' />";
} else{ $meta = apply_filters('the_content', $post->post_content);
	$meta = strip_tags($meta);
	$meta = strip_shortcodes($meta );
	$meta = str_replace(array("\n", "\r", "\t"), ' ', $meta);
	$meta = substr($meta, 0, 200);
	echo "<meta name='description' content='$meta' />";
} } add_action('wp_head', 'create_meta_desc');

The code “$meta, 0, 200” defines the size of the meta description or where it starts (0) and where it ends (200). If you think it is better to decrease or enlarge, then you can change it.

When I started writing this post I thought that merging codes for “Home” and “Single” would change something especially the last line, but I see it unnecessary.

So, just merge the two codes if you want to generate meta description for both, home and single. However …
… the final code will look like this:

function add_custom_meta_des(){
if( is_home() || is_front_page() ){
$meta_des = "Your website description here";
echo '<meta name="description" content="' . $meta_des . '" />'; } }
add_action( 'wp_head', 'add_custom_meta_des', 4 );

function create_meta_desc() {
 global $post;
if (!is_singular()) {return; }
elseif(!empty( $post->post_excerpt)) {
echo "<meta name='description' content='$post->post_excerpt' />";
} else{ $meta = apply_filters('the_content', $post->post_content);
	$meta = strip_tags($meta);
	$meta = strip_shortcodes($meta );
	$meta = str_replace(array("\n", "\r", "\t"), ' ', $meta);
	$meta = substr($meta, 0, 200);
	echo "<meta name='description' content='$meta' />";
} } add_action('wp_head', 'create_meta_desc');

See the example below. In this picture see how the code in functions.php generates meta tags in Home page and Single post.

In the post you can see the first words, the bottom of the picture.

On the right side of the picture, see how the code is located in “functions.php”.

While on the left side of the photo, see how “meta description” appears in Google search results.

You can also use a SEO plugin such as Yoast SEO, Rank Math or any other plugin. This would make it much easier for you, not only for meta description but also for other SEO features. But before using a SEO plugin you need to plan the entire structure of your site.

Share:

Related posts