Removing the paragraph tags of WordPress post excerpt and category description

Last updated on November 28, 2011. Tags: , , ,

WordPress automatically inserts paragraph tags <p> and </p> on post excerpt and category description as called for display by PHP codes the_excerpt() and category_description() respectively. Versions 2.6.1 up to 2.9.1 (the latest version when this post was last updated) insert the paragraph tags.  I'm not sure if earlier versions of WordPress also behave in this manner.

This is problematic if you intend to use them somewhere in the header tag; for example, using them as part of meta description; you will get something like:

<meta name="description" content="<p>Description goes here</p>">

To remove the automatic insertion of paragraph tags <p> and </p>, open the functions.php of your wordpress theme and insert the following:

remove_filter('the_excerpt', 'wpautop');
remove_filter('term_description','wpautop');

The first line is for excerpt while the second line is for category description. You can use only one of these lines whichever suits your needs.

Be sure that they are somewhere between the <?php and ?>, like this:

<?php
remove_filter('the_excerpt', 'wpautop');
remove_filter('term_description','wpautop');
?>

If there are other scripts in the functions.php, the safest place to insert these lines is immediately after the <?php or immediately before the ?>. If your theme has no functions.php, you can simply create it, insert the codes with <?php and ?> and save it in the same folder where you put the theme files that you currrently use.

Posted by Greten on March 28, 2009 under Hypertext Mark-up Language, WordPress

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • StumbleUpon
  • Technorati

Related Posts

You might also be interested (randomly generated):

Read Comments

  1. Posted by Jacob on 11.11.09 8:06 pm

    Awesome, thanks for this post, but I’m not sure if I have it working. I have a list of things that look like images for each entry but wordpress is putting tags inside that’s making the image fall out of the container. I tried this but it’s still not applying.

    Should I link to functions.php somewhere on the index.php page?

  2. Posted by Greten on 11.12.09 3:34 am

    There’s no need to link functions.php with index.php. The way WordPress works, they’re recognized as bundle.

    I can’t understand though what you’re saying about the images. Maybe you can show us a link.

  3. Posted by vitalous on 01.29.10 4:59 am

    It works! (I guess you knew that.)
    Thanks much for this.
    I wish WordPress was a little more liberal with the Template Tags. The force’p’ tags made the feature nearly unusable for me, as I need to disply tem ‘inline’ with surrounding content. The category descriptions can be real helpful when dealing with abbreviated and formal (longer) subject titles.
    Thanks again.
    John

  4. Posted by Suraj on 06.28.10 7:08 am

    Thanks for the tip … one question .. what if i want to remove tag from only one page template for example ‘publication’ page. is there a way to do it?

  5. Posted by Greten on 07.01.10 5:03 pm

    @vitalous – you’re welcome. I’m not against anything being “forced” i.e., default setup as long as there is a way to change them.

    @Suraj – I do not know of anyway to do that. You can remove the paragraph tag of all excerpt and then manually reinsert them on all the templates where you want them to have paragraph tags. If it’s just one particular page, you can use if function with reference to page IDs.

  6. Posted by Mark on 01.11.11 9:32 am

    Magic!

    Worked straight away – and you can’t say that about every bit of WordPress advice!

    I used it combined with a short string coded in header.php describing a site to make some description tags.

    Thanks

  7. Posted by Greten on 01.12.11 4:54 pm

    You’re welcome Mark!

  8. Posted by Arnd on 05.02.11 1:50 pm

    Tried it, the result:

    Fatal error: Call to undefined function remove_filter() in /…/wp-includes/functions.php on line 9

    Arnd

  9. Posted by Greten on 05.18.11 10:49 am

    Hi Arnd! What version are you using? I just checked one of my blogs in 3.0.3 version and it’s working fine.

  10. Posted by Sagive on 07.03.11 8:16 am

    What if i want to do that in just one place ??

    Lets say i need the meta description to be the excerpt?
    that creates validation errors..

  11. Posted by Greten on 07.07.11 10:43 am

    @Sagive – the answer to your question was already answered in this post. I’m not sure what’s causing the validation error, but if it’s the presence of p tag in meta description, then that’s what this post is all about.

Post Comments





Comment Rules and Reminders

  • The links to the commentator's e-mail do not have nofollow tag. However, I will be very strict in approving comments.
  • When you comment, please say something that indicates that you indeed read my post. If your comment is a general statement that can fit to any blog post about any topic, it will be regarded as spam.
  • What you write in the name field may include keywords to your website provided that (1) it's only up to four words long and (2) at least one of these four words is your first name or nickname. I rather reply to Bob or to Joe Smith than to Online Marketing Tips.
  • Please double check your comment before clicking the "Post" button. Once you clicked it, there will be no way for you to edit your comment.
  • Fields marked with asterisks (*) are required. Your email will never be displayed in public.