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.
Not sure if this will be helpful to anyone else but the cause of the issue was a custom formatter the theme had:
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.
Through Remove WordPress automatic formatting (see at point 9) I found out that you have to use the
raw
tags. In my case:I used NextGEN Gallery and in my templates there were
p
andbr
tags added. Another option would be to comment out the custom formatter. My theme was called MG Universal Multimedia Gallery.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.