Meta tags are a basic necessity of any website, because Meta tags tells search engines useful information about a particular page. This includes Meta title, meta description, canonical URL and other things.
If you are using WordPress as a content management system, there are lots of plugins available to show meta tags. Yoast SEO is a popular plugin which I use for my other sites.
Using WordPress plugins is fine but the problem with plugins is that often times, they slow down the site, add unnecessary codes in the <head> section of your website’s theme and call multiple URL’s in the source code, which can cause conflict with other objectives of your website. Plugins can sometimes slow down the loading time of your website and hence, it is a good practice to get the job done without using any plugins. Whenever there arises the need to use a plugin, I first try to find out if it is possible without a plugin or not.
The other day I was working on a project which required the site to load under 2 seconds. For this objective, I had to remove the wp-head() call from the head section of the wordpress theme. When i removed the wp_head() call, I found that WordPress seo plugin had stopped working.
Hence, I had to figure out a workaround to show meta tags without using any WordPress plugins and in this tutorial, I will share how you should do it too.
Show Meta tags without using WordPress plugins
First of all, we will learn how to show meta description for static pages. I will assume you do not have a large number of static pages on your website and most of them are usual blog posts. I will share how to do this for blog posts later in the article, but first, let’s see how to do it for pages.
- First of all, please disable all WordPress SEO plugins and remove the wp_head() call from your theme. We are not going to show meta information without any plugins and the wp_head() call.
- Now Copy the following code format and paste it in the <head> section of your WordPress theme
<?php $header = array( //Home Page 10 => array( 'title' => 'The Title Of Your Page Goes here', 'meta_description' => 'Meta description of the page', 'robots'=> 'noodp', 'canonical'=>'Canonical URL of the page' ), // About Page 17 => array( 'title' => 'Title of the page', 'meta_description' => 'meta description', 'robots'=> 'noindex,follow', 'canonical'=>'Canonical URL' ), );?>
This code format is initialized in the head section and it stores information of title, meta description, canonical URL and robots information in a PHP array. For every page, you have to declare an array as shown in the above example with two pages. To add more page, copy the array snippet of one page and replace the data.
Please also note the ID Of the page and you need to replace it with the ID of your page for whom you are storing this information. In the example above, I have used two pages – home page and an about page.The home page has an ID 10 while the about page has the ID 17. You will be able to find the ID of each page in your WordPress administration area
3. Now use the following code snippet to display meta tags
<title><?=$header[get_queried_object_id()]['title']?></title> <meta name="description" content="<?=$header[get_queried_object_id()]['meta_description']?>"/> <meta name="robots" content="<?=$header[get_queried_object_id()]['robots']?>"/> <link rel="canonical" href="<?=$header[get_queried_object_id()]['canonical']?>"/>
The above code will only display information that it finds declared at the top of the page. It might be a good idea to declare that information in a separate file and include that file at the top of the page using php_include(). But for the sake of simplicity of the tutorial, I have declared it at the top of the page.
Please also note that no two WordPress posts or pages can have the same ID. If a WordPress page exists with ID 1, there cannot exist a WordPress post with ID 1. So you can apply this code for both posts and pages and not only pages.
Advantages of Showing Meta tags without using any WordPress Plugin
The first advantage is that it makes no back and forth database calls.
When you store meta information of posts in database and use a plugin, that plugin makes database calls and this results in a slightly slower response. When you declare that information on top of the page, the browser can render it without waiting for the database to query it send it back to the browser.
The second advantage is that you do not have to depend on a WordPress plugin all the time. The less plugins you use, the better.
The third advantage is that you have a clean source code, which is not polluted by useless scripts being called by the plugin.
There is a disadvantage though. You have to manually add all the data in the header section and it can mean a lot of hard work, if your site is fairly big. In that case, it might be a good idea to first store all the information in a raw text file and then write a custom script to generate that code for you. Once the script is generated, you can simply paste it in the head section of the theme and it should work out.
But I would still suggest you to try this out since it is one time investment and once this is done, your site will load a little bit faster, without depending on the plugins and the database calls that it makes.
Disclaimer: I am not using this in this current site right now but I have tested this in another site and it works perfectly fine.