Can't stop p tag insertion with shortcode
-
So I’ve read several articles and posts on how to prevent WP from adding <p> in front of <img> tags. Yet, I’m unable to return shortcode content without the p tag being inserted.
My shortcode function looks like this:
function displayAllianceNews($atts, $content = null) { $thecontent.='<div>'.$content.'</div>'; // Get rid of WP <p></p> tags before <img> $content_antip = remove_ptags_images($thecontent); return $content_antip; }The input $content variable doesn’t have any <p> tags to start. It looks like:
<h2>News</h2><img src="/wp-content/uploads/2015/04/4.1.5.3-NCCRA-CBL-Cover-e1429193101216.jpg" class="wrapleftOutline" alt="photo">Here’s the filter function (just for the open p tag for testing):
function remove_ptags_images( $string ) { $patternOpen = array( '#<p>\s*<img>#' ); return preg_replace($patternOpen, '<img>', $string); }If I echo $content_antip before the return statement, the content appears correctly with no p’s. However, WP seems to be filtering the content after the return statement and adding p’s around the img tag. I added these to my functions.php as well, but to no avail:
remove_filter ('acf_the_content', 'wpautop'); remove_filter( 'the_content', 'wpautop' );Any idea what I’m doing wrong?
The topic ‘Can't stop p tag insertion with shortcode’ is closed to new replies.