Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

wordpress

I have a WordPress powered website that on the homepage uses a static page with nothing but shortcodes to generate the content.

The page gets these shortcodes by setting the front page to a static page and using the_content(); on page.php. The page content has no spaces, only shortcodes, so looks something like this:

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

It all works fine, except that WordPress adds an empty <p></p> before the shortcodes code and another P /P at the end of all the shortcodes code (Nothing in between shortcodes).

How can I remove them? I do not want to disable the global wpautop removal function though, as it can be useful for some users, I only want to remove the first and last P's that appear on the homepage.

like image 507
user1202292 Avatar asked Mar 02 '13 17:03

user1202292


3 Answers

There are a few things you can try.

You can pospone the wp_autop because it processes before the shortcode output:

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

Or use the cleanup_shortcode_fix() function that should help with your issue:

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);
like image 94
SeanWM Avatar answered Sep 24 '22 04:09

SeanWM


There are various functions aside from wpautop() that filter post content, such as force_balance_tags(), which was designed to balance bad HTML coming in via the editor.

They're mostly defined in formatting.php, where you can see the various code in source.

Removal of these filters can be as simple as one line, as you point out:

remove_filter('the_content', 'wpautop');

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

Check out below links which will help you.

  1. Removing <p> and <br/> tags in WordPress posts
  2. wordpress-wrapping-shortcodes-with-p-tags
  3. Disable wpautop Plugin

may this help you.

like image 43
Tony Stark Avatar answered Sep 22 '22 04:09

Tony Stark


Try this(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');
like image 44
Andrei Surdu Avatar answered Sep 22 '22 04:09

Andrei Surdu