Shortcode adding p and br tags

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.

3 s
3

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.

Leave a Comment