remove empty <p> tags from wordpress shortcodes via a php functon

 ̄綄美尐妖づ 提交于 2019-12-21 03:49:38

问题


Looking for a php function (non-jQuery or wpautop modification) approach to remove <p></p> from within wordpress.

I tried this but it does not work:

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

回答1:


Try inserting this code in your functions.php file:

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




回答2:


add_filter('the_content', 'cleanup_shortcode_fix', 10);

I found that it works if you specify 10 as the priority; no other number will work.




回答3:


This is an old question, but I solved this today and thought I'd share.

In my case, I basically want to remove all the poorly formatted <p> and <br> tags, but then you want to add them back in correctly so that the text in the shortcode gets formatted correctly.

/*
 * Half column shortcode
 */
    function custom_shortcode_half_column( $atts, $content = '') {
        $content = custom_filter_shortcode_text($content);
        return '<div class="half-column">'. $content .'</div>';
    }
    add_shortcode( 'half-column', 'custom_shortcode_half_column' );


/*
 * Utility function to deal with the way WordPress auto formats text in a shortcode.
 */
    function custom_filter_shortcode_text($text = '') {
        // Replace all the poorly formatted P tags that WP adds by default.
        $tags = array("<p>", "</p>");
        $text = str_replace($tags, "\n", $text);

        // Remove any BR tags
        $tags = array("<br>", "<br/>", "<br />");
        $text = str_replace($tags, "", $text);

        // Add back in the P and BR tags again, remove empty ones
        return apply_filters('the_content', $text);
    }

This really should be the default way WordPress parses the shortcode $content parameter in my opinion.




回答4:


maybe a regex could work:

$string=preg_replace_('/<p>\s*</p>/', '', $string);

That should replace any <p></p> with nothing or just whitespaces in it to nothing, thus removing them.

When applying regex to HTML code it's a good idea to remove the \r\n of the HTML first, since they stop the regex from working.




回答5:


You should increase the priority of the filter.

This should work

add_filter('the_content', 'cleanup_shortcode_fix', 1);



回答6:


You can remove

Tag enter

<?php echo $post->post_content; ?>

instead of the_content()




回答7:


What you need is a mix of jquery and php... that is the only working way
that i found to be working really well. i have tutorial on my site but
in order to keep stuff inhouse here goes

The jQuery:
include this in some JS file you already enqueue

jQuery(function($){
    $('div#removep > p').filter(function() {
        return $.trim($(this).text()) === '' && $(this).children().length == 0
    })
    .remove()
})

A shortcode you can later use:
in your functions.php or included file

function sght_removep( $atts, $content = null ) {return '<div id="removep">'.do_shortcode($content).'</div>';}
add_shortcode('removep', 'sght_removep');

Now you can wrap specific stuff like this:

[removep]
Some text i write directly in wordpress wysiwyg
<p></p> <-- this would get removed
[/removep]

This solution requires some know how but it works!
Hope this helps...



来源:https://stackoverflow.com/questions/13510131/remove-empty-p-tags-from-wordpress-shortcodes-via-a-php-functon

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!