0
votes

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=&quot;25&quot; title=&quot;Contact&quot;]" />

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?

3

3 Answers

0
votes

What if you use the strip_shortcode function try this one

function metadesc($pid) {
$p = get_post($pid);
$description = strip_tags($p->post_content);
$description = str_replace ("\n","",$description);
$description = str_replace ("\r","",$description);
$description =strip_shortcodes($description  );
if(empty($description )){

return please get home page content
}
else{
if (strlen($description) > 135) {
return htmlspecialchars(substr($description,0,135) . "...");
}else{
return htmlspecialchars($description);
 }

}

}

strip_shortcodes

0
votes

You're jumping in and out of PHP too often, which leads to coding mistakes and slow execution. Rewrite your first code:

<?php
echo '<meta name="description" content="' .
  ((some condition)? metadesc($post->ID): bloginfo('description')) . '" />';
?>

Now, if your raw data for the content is [contact-form-7 id=&quot;25&quot; title=&quot;Contact&quot;] what are you trying to turn this into? How would you like to see it reformatted? This is coming from the metadesc() function? I don't think that HTML entities in a description tag are going to be expanded to their characters, but will be used as-is. So, you might need to output [contact-form-7 id=\"25\" title=\"Contact\"] instead. Regardless, that's a very poor description -- what do you really want there?

Also be aware of whether you're in UTF-8 or a single byte encoding such as Latin-1, which becomes important when using substr() (you don't want to chop in the middle of a multibyte UTF-8 character). Also, if you're adding an ellipsis (...), you would want to take 132 characters and not 135.

0
votes

I did something similar to this, conditionally taking the excerpt and using it as the description if the viewer is on a single post page & there is an excerpt. Here's the code:

<?php
if (is_single() && $post->post_excerpt != “”) {
$post = $wp_query->post;
$descrip = strip_tags($post->post_excerpt);
echo ‘<meta name=”description” content=”‘.$descrip.’”>’;
}
?>

I also wrote a blog post detailing the whole thing.