I have page with news. I'm displaying them as a shortened text (300 chars limit), I'm removing any images using preg_replace() and everything looks fine, until someone uses line breaks in his post. Things gets messy then and my height is way to high sometimes. Ofc I'm using overflow: hidden; but its not as good as I would like it to be. Is there a way to trim out those spaces, remove <strong> if someone uses it etc.? So my news page looks always as awesome as it should?


1 Answer 1


If you run a wp_kses() on the_excerpt() (through a filter) you can whitelist specific HTML tags and such. In your case, the code would probably look something like this:

if( is_page( 'your_page' ) ) {
    add_filter( 'the_excerpt', 'wpse44363_filter_the_excerpt' );
function wpse44363_filter_the_excerpt( $excerpt ) {
    // this allows no html, you can allow only links or something of that nature
    $allowed_html = array();
    return wp_kses( $excerpt, $allowed_html );
  • Useful function, thanks. So there is just one problem now. How can I change default p's added to the content into br's after user presses enter key? I know I can remove p's by removing filter wpautop, but how to add br's? Is this the right function to override?
    – smogg
    Mar 4, 2012 at 12:43
  • 1
    You could probably do it with a string replace on </p> and a preg_replace on <p>, but I don't know why you would want to do that. You're much better off changing your CSS to do the line breaks and spacing. It's MUCH more flexible.
    – mor7ifer
    Mar 4, 2012 at 13:04
  • Actually, you're right. I don't know why I've tried to do such complex things when I can use just CSS. Anyway, thank You very much!
    – smogg
    Mar 4, 2012 at 17:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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