WordPress is a very powerful tool and it is built for SEO success if used correctly. But there are some key features that are necessary for good search results and are not part of the core WordPress functionality. In this article we are going to focus on one of the most important SEO features as I am going to show you how to implement meta description in WordPress without any plugins.
For more detailed information on how to get effective WordPress SEO without any plugins, click on the link.
The Importance Of Meta Description
Most SEOs argue that meta description is not a ranking factor by itself, but as this is what visitors actually see before deciding whether they click on your website or not, it is one of the most important factors to get good results from SEO.
Unfortunately no WordPress core functionality have been implemented for displaying a meta description in the HTML code, and themes usually don’t handle this either. What this means is that you need to implement it yourself with custom code.
This is not that difficult though as it sounds, especially in case of regular pages and posts. And that’s where you need meta description the most. What can be a bit challenging is to have a custom meta description on your homepage and on taxonomy pages. But I also have a solution for that.
Displaying Meta Description On WordPress Posts And Pages
The easiest way to display meta description on a page or post is to use a custom field to set the meta description text and simply write that out in the <head> part of your code.
Put the following code in your theme’s functions.php file at the end. In order to work, set a custom field named ‘meta description’ in your WordPress page or post.
// Display meta description as custom field on pages and posts. // By Balazs Szilagyi, https://wpseo.tips/display-meta-description-in-wordpress-without-plugin/ function custom_display_meta_description() { if (is_singular()) { $meta_description_string = "<meta name=\"description\" content=\""; $meta_description_cf = get_post_meta(get_the_ID(), "meta description", true); if ($meta_description_cf) { $meta_description_string .= $meta_description_cf; } $meta_description_string .= "\" \> "; echo $meta_description_string; } } add_action('wp_head', 'custom_display_meta_description');
What this code does is it always displays a meta description line in the HTML code of singular WordPress content, i.e. pages and blog posts. If the custom field is not set, the meta description content will be empty. If the custom field called ‘meta description‘ is set, that text will be printed out as an HTML meta description tag in the source code.
WordPress Homepage Meta Description
In WordPress, you have two options to display your homepage, and they require a slightly different approach if you want to print out a meta description without using any plugins. This is what you can set up under the Settings >> Readings menu >> Your homepage displays.
1. Using A Static Page For Your Homepage
This scenario has a very easy solution, as basically we are back at the previous step.
If you set up your homepage to be a WordPress page, you don’t need to do anything special to display a meta description on that page without plugins, just follow the prevoious step.
Your homepage will become a WordPress page, there you can set your meta description as a custom field, and the PHP code will handle it in the HTML source.
2. Using Your Latest Posts As Your Homepage
This is the most common scenario for most WordPress sites, and also the default setting. As there is no static page set up to be displayed as your homepage, our solution of displaying a custom field as meta description would not work.
Instead you have several options, depending on how customizable you want your homepage meta description to be.
Obviously the most professional solution would be to have a custom menu option in the admin area, where an admin user could set up a meta description text for the homepage, and that would be displayed in the source code. This is not that difficult to code if you are an experienced WordPress developer, but it exceeds the scope of this article.
Another, much more easy, solution is to simply hardcode your homepage meta description to your theme’s functions.php file. This might not be the most elegant solution, but will do a good job for 98% of WordPress websites. And ultimately this is a very easy to implement solution.
Simply put the following code in your theme’s functions php file at the end. Change ‘Homepage meta description text goes here‘ to whatever you want to be displayed as meta description on your homepage.
// Display meta description on pages, posts and homepage. // By Balazs Szilagyi, https://wpseo.tips/display-meta-description-in-wordpress-without-plugin/ function custom_display_meta_description() { $meta_description_string = "<meta name=\"description\" content=\""; if (is_singular()) { $meta_description_cf = get_post_meta(get_the_ID(), "meta description", true); if ($meta_description_cf) { $meta_description_string .= $meta_description_cf; } } elseif (is_home()) { $meta_description_string .= "Homepage meta description text goes here"; } $meta_description_string .= "\" \> "; echo $meta_description_string; } add_action('wp_head', 'custom_display_meta_description');
Note that we extended our previous code, so this snippet handles both singular content and your homepage. Also note that this code will output an empty meta description line even on taxonomy pages, see next part.
Meta Description On Taxonomy Pages
To have a meta description on your taxonomy pages in WordPress you have two different approaches. The first one is much easier, we will cover that in this chapter. But please note that for a full functionality, e.g. displaying custom content on taxonomy pages, you would need the second option. Click here to find out how to do that.
- Use the description field from your taxonomy page to output as meta description text.
- Add custom field functionality to your taxonomy pages.
Here is your code for solution #1:
// Display custom meta description in WordPress without plugins. // By Balazs Szilagyi, https://wpseo.tips/display-meta-description-in-wordpress-without-plugin/ function custom_display_meta_description() { $meta_description_string = "<meta name=\"description\" content=\""; if (is_singular()) { $meta_description_cf = get_post_meta(get_the_ID(), "meta description", true); if ($meta_description_cf) { $meta_description_string .= $meta_description_cf; } } elseif (is_home()) { $meta_description_string .= "Homepage meta description text goes here"; } elseif (is_tag() || is_tax() || is_category()) { remove_filter('term_description','wpautop'); $meta_description_string .= term_description(); add_filter('term_description','wpautop'); } $meta_description_string .= "\" \> "; echo $meta_description_string; } add_action('wp_head', 'custom_display_meta_description');
We are again expanding the functionality here, so this code contains everything from above and handles meta description on every type of WordPress content without a plugin.
The remove_filter and add_filter lines are a hack, because WordPress includes the opening and closing <p> tags for the taxonomy term description by default.
Please note that modifying your WordPress php files requires programming knowledge. Only use these codes at your own risk.