I'm currently using a conditional meta tag code in wordpress. Everything is working fine, besides on certain pages.
code in header.php:
<meta name="description" content="<?php echo metadesc($post->ID); ?>" />
<?php }else{ ?>
<meta name="description" content="<?php bloginfo('description'); ?>" />
<?php } ?>
code in functions.php:
function metadesc($pid) {
$p = get_post($pid);
$description = strip_tags($p->post_content);
$description = str_replace ("\n","",$description);
$description = str_replace ("\r","",$description);
if (strlen($description) > 135) {
return htmlspecialchars(substr($description,0,135) . "...");
}else{
return htmlspecialchars($description);
}
}
This is what it shows when I go to the source and look at the meta tag description on the following pages:
home: (description of the home page that is defined in Wordpress general settings (check)
biography: first 135 characters of the page (check)
contact:
<meta name="description" content="[contact-form-7 id="25" title="Contact"]" />
As you can see, I only have a contact form on my contact page and it looks like I need to add a filter to the script so that it ignores script tags and short codes, and that it will place the homepage description instead.
How can I fix this issue?