WordPress – how to add filter to description meta tag

If you using Yoast seo plugin, then you can use its filter wpseo_metadesc to change meta description tag as per your needs. Typically you want to append your site name and some more information to it. It is better to do it using filter. That way making change will be easy and at once place. Here is the filter sample code for this:

function my_wpseo_metadesc($metadesc) {
  $blog_desc = get_bloginfo('description');
  if (is_singular() && in_array(get_post_type(), array('page', 'post'))) {
    if ($metadesc) {
      $metadesc1 = $metadesc . " - " . get_bloginfo('name') . $blog_desc_suffix;
      $metadesc2 = $metadesc . " - " . get_bloginfo('name');
      $metadesc = strlen($metadesc1) <= 160 ? $metadesc1 : $metadesc2;
    }
  }
  return $metadesc;
}
add_filter('wpseo_metadesc', 'my_wpseo_metadesc');

Share this article: share on Google+ share on facebook share on linkedin tweet this submit to reddit

Comments

Click here to write/view comments