1

I just encountered weird problem. My shortcode output which is something like

'<span class="link_container"><a href="#">'.$content.'</a></span>    
<div class="upgrade_box">
some more divs here
</div>'

is being broken by random </p> closing tag inserted after </span>

Changing wpautop priority didn't work. The only solution that seemed to work was removing wpautop filter altogether which is obviously not that great of a solution.

P.S. I'm using wp 2015 theme on my testing site so it's not some theme forest theme ppl seem to have problems with.

3
  • Why was it broken? Could you provide complete example of variable's content and such? – Rarst Jan 18 '15 at 16:08
  • I assume because returned value of shortcode (mentioned in question) is passed through wpautop for some weird reason and since div shouldn't be inside p tag, it closes p right before div? – Igor Yavych Jan 18 '15 at 17:04
  • What does your actual shortcode look like? – Aibrean Jan 20 '15 at 14:48
1

Try this function out, be sure to add your shortcode in the array

// (OPT) STOP SHORTCODES THAT DON'T USE INLINE CONTENT FROM BEING WRAPPED IN A P TAG (until WP fixes this)

// ** NOTE -> BE SURE TO change the array to the shortcodes you are using!

add_filter('the_content', 'the_content_filter');
function the_content_filter($content) {
    // array of custom shortcodes requiring the fix
    $block = join("|",array( 'shortcode_name' ));
    // opening tag
    $rep = preg_replace("/(<p>)?\[($block)(\s[^\]]+)?\](<\/p>|<br \/>)?/","[$2$3]",$content);
    // closing tag
    $rep = preg_replace("/(<p>)?\[\/($block)](<\/p>|<br \/>)?/","[/$2]",$rep);
    return $rep;
}
7
  • Doesn't seem to do anything – Igor Yavych Jan 18 '15 at 18:04
  • can you include the function you used to create your shortcode? – NickFMC Jan 18 '15 at 18:19
  • pastebin.com/6RjvwY2J . Part that returns shortcode output – Igor Yavych Jan 18 '15 at 18:30
  • you are adding the shortcode like so correct? and added the shortcode_name section to the function above add_shortcode( 'shortcode_name', 'coupg_shortcode_handler' ); – NickFMC Jan 18 '15 at 20:07
  • yes, that is right – Igor Yavych Jan 18 '15 at 20:18
0

Going off @nickfmc's comment, here is a revised version for latest WordPress that uses the correct priority on the filter and also fixes a couple issues with the regex.

Note: it is best to keep each block format shortcode on its own line (separate paragraph in WYSIWYG editor).

<?php

add_filter('the_content', function ($content) {
    $blocks = 'columns|column';

    $replace = [
        "/(?:<p>)?\[($blocks)(\s[^\]]+)?\](?:<\/p>|<br\s*\/?>)?/" => '[$1$2]',
        "/(?:<p>)?\[\/($blocks)\](?:<\/p>|<br\s*\/?>)?/" => '[/$1]',
    ];

    return preg_replace(array_keys($replace), array_values($replace), $content);
}, '10.1');

Of course, replace the $blocks variable with your own shortcodes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.