Add & Display Meta Keywords Tags for Pages

Last year I wrote an articles explaining how to register custom taxonomy. Usage of such taxonomy can be extended in many different ways. Even though by default WordPress pages doesn’t require to add tags, you can easily achieve that if you want to. Today in this post I will focus on this issue and show how to get it done.

This feature can particularly be useful if you want to add tags for your pages. Another thing is, if you want to display these tags, on your meta keyword tag, that can also be done pretty easily. Here is the sample of our keywords meta tag.

<meta name="keywords" content="keyword1, keyword2">

So, let’s get started. First thing first, let’s register a custom taxonomy for pages.

Register Taxonomy

Registering taxonomy is fairly simple and easy to do. Here is the snippet.

function register_taxonomy_pages() {
   register_taxonomy('pagetag','page', array(
      'labels' => array(
         'name' => 'Tags',
	 'singular_name' => 'Tag',
	 'search_items' =>  'Search Tags',
	 'all_items' => 'All Tags',
	 'parent_item' => 'Parent Tag',
	 'parent_item_colon' => 'Parent Tag:',
	 'edit_item' => 'Edit Tag', 
	 'update_item' => 'Update Tag',
	 'add_new_item' => 'Add New Tag',
	 'new_item_name' => 'New Tag',
	 'menu_name' => 'Tags'),
      'hierarchical' => false,
      'query_var' => true,
      'public' => true,
      'show_admin_column' => true,
      'sort' => '',
      'rewrite' => array('slug' => 'page-tag','with_front' => false),
      'show_ui' => true)); }	

This snippet is identical to the one that I have mentioned earlier. However, there are differences as well. If you take a look at the register_taxonomy function, you can see that it takes three different parameters. This is exactly where we need to get creative. On our function (third line), we did the same. You can define pretty much any word to be your taxonomy. However, you should not use the reserved terms as your taxonomy.

In terms of object, it should be “Page” as I mentioned earlier that we would be registering custom taxonomy for pages. The label portion should be the way I wrote and no other change is necessary. Simply copy and paste the snippet on your theme’s functions.php page and update it.

Just in case, if you want to organize and display posts under any given taxonomy term, you need to have taxonomy-pagetag.php or at least taxonomy.php page on your site.

Retrieving Keywords

In order to retrieve keywords for pages and to display them on our keywords meta tag, we can make use of the following snippet.

function retrieve_page_terms() {
   if (is_page()) {
   $terms = get_the_terms($post->ID,'pagetag');
   if (!empty($terms)) {
      $t = array();
      foreach ($terms as $term) {
         $t[] = $term->slug; }
      $generate = implode(', ', $t);
   return $generate; } } }

Simply copy and paste this snippet on your theme’s functions.php page and update it as usual. Once you are done, call this function on your meta keywords tag from header.php page like this.

<meta name="keywords" content="<?php echo retrieve_page_terms(); ?>">

You are all set, now feel free to add some keywords for some of your pages to test my code. I am taking questions now.

Note: Do not forget to change the taxonomy on your “retrieve_page_terms” function (fourth line) if you registered a different taxonomy.

Today In History



Leave a Reply

Note: Convet HTML, PHP, JavaScripts from HTMLify, before posting from comment section.
License: By submitting a comment here you grant this site a perpetual license to reproduce your words and name/Web site in attribution. Please use your real name or a pseudonym (i.e., pen name, alias, nom de plume) when commenting. If you add your site name, company name, or something completely random, I'll likely change it to whatever I want.