wordpress widget addng extra p tags

2019-08-29 05:46发布

I have a short code like this:

function requestaquote($atts, $content = null){
    extract(shortcode_atts(array(
        'text'=>'',
        'link'=>'',
        'colour'=>''
        ), $atts));     
    return '<div class="speed-button"><img src="'.get_stylesheet_directory_uri().'/images/request-a-quote.jpg" alt="request a quote " /><p class="requstaquote">'.esc_attr($text).'</p></div><!--speed-button-->';
}
add_shortcode( 'quotetext', 'requestaquote' );

It's working except that its introducing extra <p></p> pairs like this:

<div class="textwidget">
<p></p>
<div class="speed-button">...</div>
<p></p>
</div>

which is messing up my formatting.

I have tried remove_filter('the_content', 'wpautop');

How do I removed these <p></p> pairs.

标签: wordpress
2条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-08-29 06:25

simply remove the new lines as it appears that the new lines are whats being made into paragraphs.

return str_replace("\r\n", '', $content);
查看更多
等我变得足够好
3楼-- · 2019-08-29 06:49

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:

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

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

查看更多
登录 后发表回答