Home Forums General Issues Removing Paragraph Tags


Removing Paragraph Tags

  • Hi there,

    Sorry if this has already been covered elsewhere but is there a way to get rid of the close paragraph tag after the opening of the shortcode and the open paragraph tag before it is closed? Is really messing with the styling and I know I can do it through the HTML editor but it is functionality for a client so don’t really want to let them loose there!



  • Are you using a Wysiwyg field for a shortcode alone? Or is the field being used for content and shortcodes?

    If it’s shortcode only, you could try changing your Wysiwyg field to a standard text field, then use the following PHP to activate the shortcode this way:

    $shortcode = get_field('my_shortcode');
    echo do_shortcode($shortcode);

    Untested, but should work.

  • Hi @Sour Mash Steve

    Thanks for the quesiton. Can you please test the difference between a WYSIWYG field and the native the_content?

    Do both values on the front end render the same? Or is ACF adding extra p tags?

  • Hey Elliot,

    I tried it on a normal page using the normal editor and it rendered fine without adding in the extra tags … only seems to be when using the WYSIWYG field for the custom fields.



  • Hey rmdev … is for content as well. Have tried the standard solutions (well … those from the internet anyway) regarding filters and the order in which they run but this does not sort the problem either.

  • Hey again Elliot,
    I have taken the shortcodes out and disabled the associated plug-in I was using.
    Even without these it seems that when rendered through the front end any content added through ACF WYSIWYG is adding in P tags in what appears to be a pretty random pattern … sometimes it does and sometimes it doesn’t.

  • I have this issue as well. I have actually come across it before and was able to correct it by adding this to my functions.php:

    remove_filter( ‘the_content’, ‘wpautop’ );
    add_filter( ‘the_content’, ‘wpautop’ , 12);

    I’m currently moving all of my data over to ACF and these lines of code don’t correct the issue. Ideas?

  • Hey again

    First off please ignore my comment July 10, 2013 at 7:46 pm … this was just me being stupid.

    I tried that as well revconcept … I understand what it’s trying to do and worked perfectly when using shortcodes within the standard content editing area but I think (not certain at all though!) that ACF does not use ‘the_content’ and so it is basically having no effect.



  • Hi @Sour Mash Steve

    Thanks for the info. I’ll do some more thourough testing to get this fixed.

    Can you please paste the WYSIWYG value (including shorcodes) here and also the shortcode plugins used? I’ll replicate the same issue and have it fixed in no time

  • Hi Elliot,

    I have the same problems with <p> tags appearing around images and around short code. If I start with the code in the wordpress visual or text editor it looks perfect (now that I have the following in my functions.php

    function filter_ptags_on_images($content){
       return preg_replace('/<p>\s*(<a .*>)?\s*(<img .* \/>)\s*(<\/a>)?\s*<\/p>/iU', '\1\2\3', $content);
    add_filter('the_content', 'filter_ptags_on_images');

    but in the custom fields I cannot seem to shake those extra paragraph tags. I use the html mode to remove them, update and they appear again.

    I’m having similar trouble with shortcodes, but those don’t seem to be working in the wordpress editor either. Same problem happens in both places.

    If you have any suggestions I’d really appreciate it.


  • Hi @BelindaJ

    ACF does not use the the_content filter. It does, however, use a similar filter called ‘acf_the_content’.

    Perhaps if you use this filter as well, your issue will be solved

  • Thanks Elliot! My php isn’t too hot though, do I just change ‘the_content’ to ‘acf_the_content’ ?



  • Hi @BelindaJ


    add_filter('acf_the_content', 'filter_ptags_on_images');
  • Hi Elliot, sorry I’m a bit hopeless when it comes to this stuff… I tried

        function filter_ptags_on_images($content){
       return preg_replace('/<p>\s*(<a .*>)?\s*(<img .* \/>)\s*(<\/a>)?\s*<\/p>/iU', '\1\2\3', $content);
    add_filter('the_content', 'remove_empty_p', 20, 1);
    function remove_empty_p($content){
    $content = force_balance_tags($content);
    return preg_replace('#<p>\s*+(<br\s*/*>)?\s*</p>#i', '', $content);

    but it didn’t work, is the $content part supposed to be different?

    Thanks for your help,


  • Although, it’s not just images, I have paragraph tags around every shortcode as well, so the shortcodes aren’t doing what they need to do. (UI Tabs)

  • Hi @BelindaJ

    I can’t see that you have changed the filter name from the_content to acf_the_content.

    I don’t understand why you have included a remove_empty_p function either…

  • This reply has been marked as private.
  • Hi @BelindaJ

    I think you need to re-read my previous advice. I have specified the exact line which needs to be changed. I don’t understand why you have renamed a parameter. this would do nothing.

    I am not intending to be rude, but ACF is a developer tool and as a per-requisute you must understand how to use WP actions, filters and an understanding of function paramters, etc.

    There’s not much else I can do from my end. I have already given the solution in a previous comment

  • Hi Elliot,

    It didn’t work because I still needed that function for the content area, so I needed an additional function.

    I found a link on your old forum half an hour ago that solved all my issues

    I was getting p tags around images, and shortcodes – causing lots of errors.

  • I’m having same issue and want to remove p tag and for me it appears to be brought on by TinyMCE Advanced setting that is checked to Stop removing <p> adn <br/> tags when saving and show them in the HTML editor.

    When I uncheck this and resave ACF page the <p> goes away.

    I still want to have this TinyMCE box checked but I think that may be causing it.

  • Hi @ottomek

    To save me from re-reading the whole thread, can you please create a new topic and clearly describe the issue so I can help solve it.

    I think this thread has had a few too many directions to be useful to future readers.


  • If anyone else is looking for a solution to removing <p> tags from around images, in the ACF WYSIWYG editor, just use the following code in your functions:

    function filter_ptags_on_acf_images($acfcontent){
    		return preg_replace('/<p>\s*(<a .*>)?\s*(<img .* \/>)\s*(\/a>)?\s*<\/p>/iU', '\1\2\3', $acfcontent);
    	add_filter('acf_the_content', 'filter_ptags_on_images');
  • Here is the solution I have used to remove the stray p tags when using shortcodes in the ACF WYSIWYG (put that in your functions file).

    function wpex_clean_shortcodes($content){   
        $array = array (
            '<p>[' => '[', 
            ']</p>' => ']', 
            ']<br />' => ']'
        $content = strtr($content, $array);
        return $content;
    add_filter('acf_the_content', 'wpex_clean_shortcodes');?>

    So far, I have not had any problems with this solution.

  • Hi @tebbott: Could you help me adapt this function? I want to apply it to a slightly different scenario, but my regex chops aren’t good enough:

    function filter_ptags_on_acf_images($acfcontent){
    		return preg_replace('/<p>\s*(<a .*>)?\s*(<img .* \/>)\s*(\/a>)?\s*<\/p>/iU', '\1\2\3', $acfcontent);
    	add_filter('acf_the_content', 'filter_ptags_on_images');

    Function output:

    <a href="#"><div>

    Generated markup:

    <a href="#"><div>

    How can I strip out that annoying p tag?

    I’m also using a version of the wpex_clean_shortcodes() function listed above. It works well for some scenarios but it’s not helpful here. Can your regex function help me?

  • I’m having the issue with shortcodes being wrapped inside p and followed by br.

    solution doesn’t seem to work for me. I’m using Custom Content Shortcode, if that matters.

    Using ACF Pro 5.3.7

Viewing 25 posts - 1 through 25 (of 25 total)

The topic ‘Removing Paragraph Tags’ is closed to new replies.