P for formatting tags in short Wordpress code

I know that this must have been asked a hundred times, but I tried to find many solutions that could be found, but no one works.

I have a shortcode that wraps the contents:

 [important_note]Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam vel velit at ex congue aliquet.[/important_note] 

I use the following shortcode functions.php in functions.php :

 function important_note( $atts, $content = null ) { return '<div class="imp-note">'.$content.'</div>'; } add_shortcode("important_note", "important_note"); 

This is the resulting html code:

 <div class="imp-note"> Lorem ipsum dolor sit amet, consectetur adipiscing elit. <p></p> <p>Nam vel velit at ex congue aliquet.</p> </div> 

The main problem is the empty <p> , which I tried to solve using the solutions mentioned here: remove empty <p> tags from Wordpress shortcodes via php functon and Github , however this seems to solve the <p> shortcode wrapping and not the shortcode content .

If the first line without any <p> wrapping is also odd, what makes me think that I should add code to the shortcode function?

Thanks in advance for your help.

UPDATE

Ok, so this bit of code looks like a trick:

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

As shown here: https://stackoverflow.com> )

And the content in the short code should be as shown above:

 [important_note] Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam vel velit at ex congue aliquet. [/important_note] 

Content is separate from shortcode tags. This shortcode outputs the following html:

 <div class="imp-note"> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p> <p>Nam vel velit at ex congue aliquet.</p> </div> 

Will check if it works in all shortcodes ...

+8
php wordpress shortcode
source share
2 answers

As I mentioned in my question. Updating this bit of code in functions.php does the trick:

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

As you can see from this question, answer: https://stackoverflow.com/a/166269/ and this post: http://www.paulund.co.uk/remove-line-breaks-in-shortcodes ( scroll down to: Uninstall wpautop () for content in shortcodes only)

It is also important to have the content in the short code separately from the short code tags, otherwise the first line will not be wrapped with the <p> :

 [important_note] Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam vel velit at ex congue aliquet. [/important_note] 

HTML output:

 <div class="imp-note"> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p> <p>Nam vel velit at ex congue aliquet.</p> </div> 

I checked the shortcodes on the WordPress site and they all have the correct formatting. Hope this helps a little look there :)

+2
source share

try it

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

For more information: http://codex.wordpress.org/Function_Reference/wpautop

And also you can add this function to functions.php file

 add_filter('the_content', 'remove_empty_p', 20, 1); function remove_empty_p($content){ $content = force_balance_tags($content); return preg_replace('#<p>\s*+(<br\s*/*>)?\s*</p>#i', '', $content); } 
+1
source share

All Articles