Remove the automatically added <p> from a page that has no literal content (uses shortcodes)

I have a WordPress-enabled website that uses a static page on the homepage where there is nothing but shortcodes for creating content.

The page receives these shortcodes by setting the front page to a static page and using the_content (); On the page. The content of the page has no spaces, only short codes, so it looks something like this:

[content-shortcode blah blah][more content-shortcode blah blah] 

Everything works fine, except that WordPress adds an empty <p></p> in front of the shortcode code and another P / P at the end of the whole shortcode code (nothing between shortcodes).

How can I delete them? I do not want to disable the global wpautop removal function, although, since it may be useful for some users, I want to remove only the first and last P that appear on the main page.

+6
source share
5 answers

There are a few things you can try.

You can cancel wp_autop because it processes before outputting the short code:

 remove_filter( 'the_content', 'wpautop' ); add_filter( 'the_content', 'wpautop' , 12); 

Or use the cleanup_shortcode_fix() function, which should help in your problem:

 function cleanup_shortcode_fix($content) { $array = array('<p>[' => '[', ']</p>' => ']', ']<br />' => ']', ']<br>' => ']'); $content = strtr($content, $array); return $content; } add_filter('the_content', 'cleanup_shortcode_fix'); $string = preg_replace_('/<p>s*</p>/', '', $string); add_filter('the_content', 'cleanup_shortcode_fix', 1); 
+19
source

There are various functions besides wpautop() that have a post content filter , such as force_balance_tags() , which was designed to balance the bad HTML coming in through the editor.

They are mainly defined in formatting.php , where you can see various codes in the source.

Removing these filters can be as simple as a single line, as you specify:

remove_filter('the_content', 'wpautop');

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

Check out the links below to help you.

can it help you.

+2
source

Try it (paste this code somewhere in functions.php ):

 function shortcode_empty_paragraph_fix($content){ $array = array ( '<p>[' => '[', ']</p>' => ']', ']<br />' => ']' ); $content = strtr($content, $array); return $content; } add_filter('the_content', 'shortcode_empty_paragraph_fix'); 
+2
source

Since you want to remove only the <p> tags on the home page, add the following code to your functions.php file:

 add_action('pre_get_posts', 'remove_autop_from_home'); function remove_autop_from_home() { // check to see if we are on the home page if(is_home()) { remove_filter('the_content', 'wpautop'); } } 
+1
source
 jQuery('p:empty').remove(); 

You can also use JS. The above code will help you remove all empty p tags from the page

0
source

All Articles