5

I apologize as I realize this has been asked before and I believe the issue is with wpautop function adding closing p or br tags but every solution I've found has not worked so I'm at a loss for fixing the issue.

I'm doing:

class my_plugin {

  public static function init() {
    remove_filter('the_content', 'wpautop');
    add_shortcode('myshortcode', array(__CLASS__, 'my_shortcode'));
  }

  public static function my_shortcode() {
    /* do stuff here */
    ob_start();
    include('views/file.php');
     return ob_get_clean();
  }
}

my_plugin::init();

I've also tried:

remove_filter( 'the_content', 'wpautop' );
add_filter( 'the_content', 'wpautop' , 99 );
add_filter( 'the_content', 'shortcode_unautop', 100 );

This is what's in the view file right now:

<form method="get" id="search" action="<?= get_permalink() ?>">
    <input name="st" id="st" class="text" type="text">
    <input id="search" class="submit" value="Search" type="submit">
</form>

And here is what's being rendered:

<form method="get" id="search" action="">
    <input name="st" id="st" class="text" type="text"><br>
    <input id="search" class="submit" value="Search" type="submit"><br>
</form>

If I take the original version and remove all line breaks it renders correctly. The issue is it's a significantly simplified version of original file and it would make editing "views" a nightmare if they had to be on a single line.

For what it's worth if instead of returning the view content I echo it out everything works as expected (except of course being able to place the shortcode in other content correctly).

Also the do stuff here part is just getting two variables from the request and one API call to set up the variables used in the view.

7
  • Have you tried placing the remove_filter() code in the my_shortcode() function? May 15, 2013 at 22:35
  • Charles - Yes, and unfortunately that didn't work either. I've written shortcodes before that did not have this issue using the same technique (in fact on the same site).
    – JSP
    May 15, 2013 at 22:41
  • Have you tried removing all the filters? remove_all_filters( 'the_content' ); May 15, 2013 at 22:48
  • Charles - With that I don't even get the shortcode tag replaced. My page has [myshortcode] on it.
    – JSP
    May 15, 2013 at 23:11
  • That makes sense. mindctrl (in Answer), is right, without seeing all the code, we cannot reproduce your results to run our own tests. The only other thing I can think of is to temporarily disable all plugins and test again. May 16, 2013 at 7:31

3 Answers 3

4

Not sure if this will be helpful to anyone else but the cause of the issue was a custom formatter the theme had:

function my_formatter($content) {
        $new_content = '';
        $pattern_full = '{(\[raw\].*?\[/raw\])}is';
        $pattern_contents = '{\[raw\](.*?)\[/raw\]}is';
        $pieces = preg_split($pattern_full, $content, -1, PREG_SPLIT_DELIM_CAPTURE);

        foreach ($pieces as $piece) {
                if (preg_match($pattern_contents, $piece, $matches)) {
                        $new_content .= $matches[1];
                } else {
                        $new_content .= wptexturize(wpautop($piece));
                }
        }

        return $new_content;
}

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

add_filter('the_content', 'my_formatter', 99);

FWIW: The theme is Avenue (from ThemeForest) which uses the "Pyre" theme framework. The reason another site that uses the same theme didn't have the issue is the functions.php file was re-written/changed as part of a child theme and did not include the shortcodes.php file which had the above function.

Sorry to waste your time but I really appreciate your time/answers.

1
  • 1
    I have the same cutom formatter in my theme. What have you done? Have you removed it? Or have you changed the code?
    – testing
    Jun 12, 2013 at 9:12
1

Through Remove WordPress automatic formatting (see at point 9) I found out that you have to use the raw tags. In my case:

[raw][album=1 template=portfolio][/raw]

I used NextGEN Gallery and in my templates there were p and br tags added. Another option would be to comment out the custom formatter. My theme was called MG Universal Multimedia Gallery.

0

Without seeing all the code it's hard to say.

I have seen an issue a few times where certain line breaks, spacing, or weird characters in the markup inside shortcode functions causes WP to automatically add those where they shouldn't be.

Looking at your function, it might be something like that inside views/file.php that's doing it. Make sure you have no weird spacing or unnecessary line breaks. Even try to delete line breaks and add them back. Sounds weird, but I've seen it before.

1
  • mindctrl - I added the view which is part of the problem as it definitely has line breaks. Removing the line breaks works adding them back in just breaks the HTML again.
    – JSP
    May 16, 2013 at 12:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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