Support » Fixing WordPress » Remove P tags when outputting content

  • Hi all,

    When pulling in content for a post, is it possible to strip the paragraph tags that are added in via the tinyMCE plugin? The reason I would want to do it at code level is that I would like the paragraph tags to be added on some pages and not on others. Such as, when I add an image, I would like to remove the paragraph tags surrounding it.

    So, here is an example of my line of code, can I add an extra condition here to remove the P tags?

    <?php while (have_posts()) : the_post(); ?>
      <a href="<?php the_permalink() ?>" rel="facebox">
         <?php the_content(''); ?>
      </a>  </li>
    <?php endwhile; ?>

    So, for example where the content is pulled in, I wouldn’t want it to be wrapped in a <p> tag as it is already inside a HREF.

    Oh, and I’ve checked the content in the database and the <p> tags are being added in there, so I want to strip them when they come back out if possible?

    Would really appreciate any help.

Viewing 11 replies - 1 through 11 (of 11 total)
  • You could wrap everything you want to change in a div tag and then in the css of the class assigned to that div you could write class=”something” and then .something P {display:none;}. Never tried that but the logic is sound (smile)!





    Thanks for your replies guys. We actually had to remove TinyMCE and add a new WYSIWIG editor, so that plugin is not working for me. I was hoping I could remove it at code level, so I could tailor it between the different sections, rather than remove paragraph formatting throughout the site.

    Anyway, thanks for your help. Jonji, your idea might be the only option, will give it a shot.


    Hi all, our PHP developer kindly fixed this for me here. Here is the line of code:

    I’m using this with the more_fields plugin, so it’s on an extra field, which is usually called in using the “meta” call. So, for example:

    <?php meta('thumbnail-graphic');?>

    This now has a few extra lines to remove all tags EXCEPT for the IMG tag, (therefore the P tag surrounding it). Here is the code:

    <?php $thumby = get_meta('thumbnail-graphic'); echo strip_tags($thumby, '<img>'); ?>

    i checked out the disable wpautop plugin. fine, unless you just want to eliminate p tags for a particular part of your template file or theme. anyway, the plugin is only the following 2 lines, which remove the automatic p tag filter for template tags in the wp-includes/default-filters.php file:

    remove_filter ('the_content',  'wpautop');
    remove_filter ('comment_text', 'wpautop');

    but you could add these lines manually to your template file like so and you’re good:

    <?php remove_filter ('the_content',  'wpautop'); ?>
    <?php the_content(__('Read more'));?>

    I used it in a situation for a page template where the_content displayed an embedded flash movie. no need for p tags! 🙂

    I used it in a situation for a page template where the_content displayed an embedded flash movie. no need for p tags! 🙂

    Hey! that was exactly the problem I was having until I read your post! thanks for sharing, it was very helpful!
    I used:
    <?php remove_filter ('the_content', 'wpautop'); ?>
    And worked like a charm! 🙂 Thanks again.


    Our developer here came up with this line of code. It uses the “more fields” plugin, which is why we’re using the “meta” option instead of “the_content”, but I’m sure that could be changed.

    Basically, it removes all tags except for the <img> tag or any other you add to the list at the end of the line of code:

    <?php $thumby = get_meta('our-work-image-one-small'); $stringo = strip_tags($thumby, '<img>'); echo twenty_add_attribute($stringo,'class','newsThumb'); ?>

    Hope that helps

    I still haven’t found a ‘per page’ option.

    I’m happy to have p’s insert automatically into posts on the blog pages but I want the option on Pages.

    Anyone know how I can get that happening?

    Thanks very much!
    I just put <?php remove_filter (‘the_content’, ‘wpautop’); ?> in a page and there were no more p tags there, but still on other pages.

    Great tip!!

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Remove P tags when outputting content’ is closed to new replies.