Mobile Number Tracker
Trace Mobile Number Earn Money Online
© TechWelkin.com

Remove P Tag from WordPress Excerpt, Content, Category Text

WordPress Codex
Samyak Lalit | June 5, 2014 (Last update: June 30, 2021)

Samyak Lalit is an Indian author and disability rights activist. He is the principal author and founder of projects like TechWelkin, WeCapable, Viklangta, Kavita Kosh among many others.

WordPress is one of the most popular content management system and blogging platforms in the world. A rough estimate says that about 20% websites on the internet are running on WordPress.

Sometimes WordPress developers face an odd problem of the post excerpt text coming closed inside a P (paragraph) tag. WordPress provides the_excerpt(); function to display excerpt of a post or a page. But when developers use this function, they notice that the output text is automatically being wrapped inside a <p> tag. In most cases this does not create any problem but in some instances where you’re trying to do a specific thing with the excerpt text —this unwanted extra paragraph tag turns out to be a nuisance.

Not only the excerpt but the output of category description and the_content(); functions also come wrapped the same way. Let us see how to get remove P tag from output content.

What causes this p tag wrapping?

WordPress applies certain filters to outputs of various functions. It is one of these filters that applies p tag around the excerpt text.

Why is it this wrapping done?

This is done probably to proactively help WP developers and display the excerpt text in a neat and clean way. But sometimes this pro-activeness of WP irritates the developers because wrapping a paragraph tag around excerpt text is easy but removing and existing tag requires efforts. Developers feel that WP should spit out raw text to enable developers to stylize it as they wish.

WordPress Codex

WordPress Codex

How to remove this extra P tag?

The best and cleanest way to get rid off this tag is to remove the filter that applies it.

Go to your functions.php file (Appearance > Editor > functions.php) and add the following code at the end of this file:

remove_filter ('the_excerpt', 'wpautop');

if you want to remove P tag from around content output, add this in the file:

remove_filter ('the_content', 'wpautop');

to remove P tag from category description, use:

remove_filter('term_description','wpautop');

Another method of removing extra P tag

There is also a way to get raw text without removing the filter. Instead of using the_except(); and the_content(); functions to get the text, you should use:

&lt;?php echo get_the_excerpt(); ?&gt;

and

&lt;?php echo get_the_content(); ?&gt;

these functions return plain and raw text without putting it through the filter mentioned above.

I hope this article helped you and save you some time. Please let me know if you have any question or comment! Thank you for using TechWelkin.

© TechWelkin.com

9 responses to “Remove P Tag from WordPress Excerpt, Content, Category Text”

  1. Khalil says:

    Thanks for sharing. I was searching for this method for a week.

  2. Kenniko says:

    Thank you Lalit. It works like charm.

  3. Maggie says:

    Thank you so much, worked like a wonder!

  4. Deniz says:

    Thank you. it works.

  5. Kaliko Trapp says:

    Thank you SO much, Lalit! This was exactly what I was trying to figure out. Even the WordPress Codex couldn’t make it as simple or as easy to understand! I just wonder why WP Core doesn’t return plain text for these items, as you mentioned also.

  6. Sreenath Sudheer says:

    Yeah..Its Working!!! Thanks dude :)

  7. Romain says:

    Thanks for this helpful trick dude. That’s what I was looking for.

    • Lalit Kumar says:

      Thank you Romain for taking time out to comment! It feels great when you can be of help to others. Stay in touch!

Leave a Reply

Your email address will not be published. Required fields are marked *