Support

Account

Home Forums Front-end Issues Shortcode being wrapped with a paragaph tag Fix

Unread

Shortcode being wrapped with a paragaph tag Fix

  • Hi all,

    Thought I would share this:

    Everytime I add in a shortcode, it wraps the shortcode in a paragraph. Also, if I add a line break within the shortcode, it adds in an empty paragraph.

    I dont have wpautop turned on, so I fixed this problem this way.

    In my template, I call my acf field, which is a WYSIWYG editor:

    <?php
                if(get_field('cf_MYFIELDNAME'))
            
                {
                    apply_filters('acf_the_content', the_field("cf_MYFIELDNAME"));
                }
                ?>    

    Then, on my functions.php, i added this:

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

    and it seemed to do the trick.

    Anyone out there, have a look at it, and if they see any errors in my logic, or it can be improved, let me know!

    Thanks,

    JT-MTL

  • Hi,

    Maybe you can try this solution:

    // Change the execution priority of wpautop so that it executes
    // after the shotcodes are processed instead of before
    remove_filter( 'the_content', 'wpautop' );
    add_filter( 'the_content', 'wpautop' , 12);
  • Hi @jt-mtl

    Thanks for the info. There are a few things with your code I would like to talk about:
    1. Always use get_field when using the value as a variable, not the_field
    2. You don’t need to apply the filter manually, this is done already by ACF
    3. Your custom filter on the ‘acf_the_content’ looks good, I think this is all that is needed (no manual apply_filters code)

    Thanks
    E

  • Hi @bobz

    This looks interesting. Should I be adding this change to the ACF acf_the_content filter? If so, can you please link to where you found this $priority modification fix?

    Thanks
    E

  • Hi E,

    Not sure, I googled this, can’t find it now.
    Someone post it on a blog.

    I was creating some shortcodes for making columns and I had trouble with this autop, this fixed my problem.

    BR
    V

  • Hi all, a year and a half later, I’m weighing in to add one bit of advice that worked for me:

    I kept getting </div></p> and similar phantom paragraph tags whenever I used shortcodes in an ACF WYSIWYG field. I extended @jt-mtl’s add_filter solution a bit and it worked for me.

    $array = array (
        '<p>[' => '[',
        ']</p>' => ']',
        ']<br />' => ']',
        '<p><div' => '[',
        '</div></p>' => ']',
        '</div><br />' => ']'
    );

    You could add any other markup to the array if you want to. Divs were all I needed, so I stopped there. Hope it helps!

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

The topic ‘Shortcode being wrapped with a paragaph tag Fix’ is closed to new replies.