Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

Cheers

share|improve this question

1 Answer 1

up vote 1 down vote accepted

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 );
}
share|improve this answer
    
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 '12 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. –  m0r7if3r Mar 4 '12 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 '12 at 17:15

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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