0

This must be pretty simple, but when I use the function below in header.php to grab the post excerpt to use a meta description for a single post or page

setup_postdata($post); $excerpt = get_the_excerpt(); echo $excerpt;

I end up with html entities for apostrophes, quotes, etc., like this:

<meta name="description" content="There&amp;#8217;s an interesting thing
going on in the world of digital music; it&amp;#8217;s moving
into the &amp;#8220;cloud.&amp;#8221; ...">

I've tried echo htmlentities($excerpt); and echo html_entity_decode($excerpt); with no luck. How can I prevent those entities from showing up? I'm not using a function in functions.php to generate an excerpt.

This is kind of a workaround, as I have hundreds of posts that don't have a manual excerpt, and I think manual excerpts are immune to the entity issue, and that's why I am trying to use the standard excerpt.

1

I think you want to to this on singular post/page, so you don't think to setup global post object, it is already set up.

After that, the problem is that calling get_excerpt will run some filters that add html entities.

This is because get_the_excerpt is intended to be used in page content (inside the <body> part) and there is a side effect, when no manual excerpt is on page, that function also call the the_content filter, that can cause some compatibility problems with plugins... so what I suggest is do not use that function to exctract the description, but use some low level functions:

After a very quick test, I think this should be good, but probably can be improved:

in functions.php

function head_description( $desc = '' ) {
  $desc = str_replace( '"', '', html_entity_decode( $desc ) );
  $desc = stripslashes( wp_filter_nohtml_kses( $desc ) );
  return str_replace( '&amp;', '&', $desc );
}

Then in header.php

<head>
<?php
if ( is_singular() ) {
  global $post;
  $excerpt = $post->post_excerpt ? : wp_trim_words( $post->post_content, 55, '' );
  $desc = head_description( $excerpt );
} else {
  $desc = head_description( 'Foo' ); // description for non singular pages
}
?>
<meta name="description" content="<?php echo $desc; ?>">
<?php } ?>

Be sure to use double quotes in content=" ... " because single quote in description are not escaped and so you have problems if you use single quote to wrap content.

  • Thanks, this works fine, and I can change the length of the excerpt, too. Maybe some others can see improvements, but I don't know. What do you mean by this: "Be sure to use double quotes in content=" ... " because single quote in description are not escaped and so you have problems if you use single quote to wrap content."? Single quotes work OK in the text that makes up the excerpt. – markratledge Nov 27 '13 at 19:32
  • Yes single quotes inside the descriptionare ok, you have to use double quotes after content=, so to wrap the description. (Any double quote inside description will be stripped out) @songdogtech – gmazzap Nov 27 '13 at 19:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.