In this tutorial, I'll show you how to add meta tags to your WordPress website. By default, WordPress doesn’t support meta tags, but with a few easy steps you can add support for meta tags in your WordPress website.
Meta tags are important when it comes to making your website SEO-friendly. When search engine crawlers visit your website, they fetch metadata information in the form of meta tags in addition to fetching actual content of that page. In fact, meta tags allow us to inform crawlers about the specifics of the web pages.
There are certain keyword meta tags that don’t contribute to search engine ranking of your website, although some meta tags like meta description can significantly improve search results.
WordPress doesn’t come with built-in support for meta tags, and thus, you need to do a bit of extra work if you want to take advantage of meta tags. Broadly speaking, when it comes to providing meta tags support in WordPress, there are two ways: either you need to edit the WordPress code files or you need to install a third-party plugin. In this article, we’ll discuss both ways.
In this section, we’ll discuss how you could enable meta tags support in your WordPress website without any plugins.
To enable meta tags support, we’ll have to add a code snippet in the functions.php file of your active theme. If you're using your own custom theme, you can edit the functions.php file directly. However, if you're using a third-party theme like Twenty Twenty, you should create a child theme before you make any changes.
You can learn how to create a child theme here:
In this example, I'll show you the code for a Twenty Twenty child theme. Go ahead and open the wp-content/themes/{YOUR_THEME}/functions.php file and add the following snippet at the end.
function twentytwenty_add_meta_tags() { // homepage if (is_home()) { echo '<meta name="description" content="' . get_bloginfo( "description" ) . '" />' . "n"; } // single post page if ( is_singular() ) { global $post; $meta_description = strip_tags($post->post_content); $meta_description = strip_shortcodes($post->post_content); $meta_description = str_replace(array("n", "r", "t"), ' ', $meta_description); $meta_description = substr($meta_description, 0, 160); echo '<meta name="description" content="' . $meta_description . '" />' . "n"; } // category page if ( is_category() ) { $meta_description = strip_tags(category_description()); echo '<meta name="description" content="' . $meta_description . '" />' . "n"; } } add_action( 'wp_head', 'twentytwenty_add_meta_tags');
In the above snippet, we’ve added the twentytwenty_add_meta_tags
custom function which will be used to add meta tags to the different pages of your website. We’ve then used the wp_head
hook to add meta tags in your pages. Of course, if you're using a different theme, replace twentytwenty_add_meta_tags
with the name of your own theme.
As you can see, we’ve implemented meta tags support for the home page, single post page and category pages. Of course, you could go ahead and modify this code as per your needs. You could add support for other types of pages similarly.
In the next section, we’ll see how you could add meta tags by using third party plugins.
In this section, we’ll see how you to add meta tags to your website with the help of the Meta Tag Manager plugin. You can install it with from the WordPress admin sidebar with Plugins > Add New, or you can download and install it manually to your wp-content/plugins directory.
Finally, activate the plugin in the Plugins section of the admin sidebar.
Once the plugin is installed, you can access the plugin UI at Settings > Meta Tag Manager.
There are two ways you could use this plugin. You can add custom meta tags for specific pages or you could enable meta tags for specific content types like post, page and media. So it depends on how you would like to configure meta tags in your website.
In this section, we’ll see how you could add the meta description tag only on the homepage. Head over to the Settings > Meta Tag Manager > Custom Meta Tags section, click on the + Add Meta Tag button and configure the meta description tag as shown in the following screenshot.
Click on the Save Changes button to save your changes.
Go ahead and check the source code of your front page ( Ctrl + U in Chrome), and you will see the following meta tag added by the Meta Tag Manager plugin!
<meta name="description" content="Just another WordPress site" />
In this way, you can add as many tags as you want to add to your pages.
The other option is to enable the meta tag builder for specific content types. For example, if you want to enable the meta tag builder for the Post content type, go to the Settings > Meta Tag Manager > General Options section. Select the Posts content type as shown in the following screenshot and hit the Save Changes button to save changes.
Now, go ahead and create a new post, and you would notice that the meta tag builder is added to it as shown in the following screenshot.
You would use it in the same way as we discussed in the previous section. In this way, you can enable the meta tag builder for different content types,