Shortcode output adding
after new line

前端 未结 2 833
误落风尘
误落风尘 2021-02-06 08:19

I\'m trying to create a shortcode to add a CSS style attribute to a page. I added the following code to my theme\'s functions.php.

function add_style( $atts, $co         


        
2条回答
  •  無奈伤痛
    2021-02-06 09:15

    The br tag gets inserted because of the default order in which WordPress processes your content – wpautop (the function which converts line breaks to p or br tags) is run before the shortcodes are processed.

    The Solution:

    Change the execution priority of wpautop so that it executes after the shotcodes are processed instead of before. Add this in your functions.php file:

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

    Now there will be no extra p or br tags added inside your shortcode block. In fact there will not be any automatic conversion of line breaks to p and/or br tags at all. So if you want the legitimate line breaks to convert to p and br tags, you will need to run wpautop from inside your shortcode function, e.g.:

    function bio_shortcode($atts, $content = null) {
       $content = wpautop(trim($content));
       return '
    ' . $content . '
    '; } add_shortcode('bio', 'bio_shortcode');

提交回复
热议问题