Last updated on February 13, 2010. Tags: category description, HTML tags, paragraph tag, post excerpt
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 (HTML), WordPress tweaks
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.
* Required. Your email will never be displayed in public.
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?
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.
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
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?
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.