WordPress has several features and functions that make it easy and convenient to use for both, novice and advanced users. One major feature to back its user-friendliness is that it provides a hybrid of WYSIWYG editor and an HTML editor to write content.
wpautop is a function that WordPress uses to ensure that content in your editor looks the same when published on your website. In our previous article we saw how to disable wpautop to avoid the unnecessary line break and paragraph tags from the_content and the_excerpt. We also remarked on how wpautop can lead to unexpected results, especially when combined with shortcodes. In this article we focus in depth on the same.
The Problem – wpautop and Shortcodes
As is its job, wpautop inserts line break and paragraph tags in the content you write. Although WordPress does try to detect shortcodes to avoid unwanted problems, wpautop still ends up messing the code you enter. You would get a <p> or a <br> tag every time you insert a new line, and the end result would be plain text in place of the shortcode.
Now you can easily solve this problem. You just have to disable the wpautop filter for your website. This solution however, works fine only if you use the HTML editor to write your content and you manually add the markup.
In case you use the visual (or the WYSIWYG) editor, you would not get any tags inserted automatically every time you hit Enter. The result would be that your content, when published will have no paragraph or line breaks as you desired.
Therefore, the solution here lies in disabling wpautop partially, i.e, you would disable wpautop but only for shortcodes and not for the_content, or the_excerpt. This way you could enter text and shortcodes both, conveniently in your posts.
Solution – Disable wpautop Only for Shortcodes
WordPress assigns priorities to its functions which regulates the order of how the different functions process. The root cause of our problem here is that wpautop function processes before the function do_shortcode does. wpautop has the default priority value of 10.
Now what we need to do is to make wpautop start after do_shortcode. To achieve this, we simply have to remove the wpautop filter first, and then add it again with a higher priority. This way it would not affect the shortcodes we enter.
Simply add the following code to your functions.php file.
remove_filter( 'the_content', 'wpautop' );
add_filter( 'the_content', 'wpautop' , 99);
Here we have set the priority value of wpautop to 99. you can choose any value higher than 10. This would make sure that wpautop runs after do_shortcode.
And you’re done! This way you retain wpautop’s functionality while avoiding unnecessary insertions. Now there will be no unwanted paragraph tags or line breaks in your shortcodes. If you need any further assistance feel free to post your doubts in the comments below. We will get back to you at the earliest.