remove empty

tags from wordpress shortcodes via a php functon

前端 未结 7 1849
余生分开走
余生分开走 2021-02-13 10:20

Looking for a php function (non-jQuery or wpautop modification) approach to remove

from within wordpress.

I tried this but it does not w

相关标签:
7条回答
  • 2021-02-13 10:58

    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...

    0 讨论(0)
  • 2021-02-13 10:59

    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.

    0 讨论(0)
  • 2021-02-13 11:03

    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.

    0 讨论(0)
  • 2021-02-13 11:09

    You should increase the priority of the filter.

    This should work

    add_filter('the_content', 'cleanup_shortcode_fix', 1);
    
    0 讨论(0)
  • 2021-02-13 11:13

    You can remove

    Tag enter

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

    instead of the_content()

    0 讨论(0)
  • 2021-02-13 11:14

    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 );
    

    0 讨论(0)
提交回复
热议问题