plugins - Prevent shortcode from being wrapped in <p> tags

I'm using a latex plugin to display formulas on my wordpress site, and I would like to add shortcodes with formulas

I'm using a latex plugin to display formulas on my wordpress site, and I would like to add shortcodes with formulas inside them, so the content of the shortcodes must be processed by the plugin before it gets displayed in the page.

For example, by writing $\frac{15}{5} = 3$ in the editor, this gets displayed in the page .

So I wrote this simple shortcode

add_shortcode( 'test', 'test_sc' );
function test_sc( $atts ){
    return "$\frac{15}{5} = 3$";
}

but by writing [test] in the editor, this gets displayed in the page $\frac{15}{5} = 3$.

So I searched to solve the problem, and found out that by writing

add_shortcode( 'test', 'test_sc' );
function test_sc( $atts ){
    return apply_filters( 'the_content', '$\frac{15}{5} = 3$' );
}

the formula is displayed correctly, BUT it gets wrapped in <p> tags, so that by writing the fraction [test] equals a natural number, this gets displayed

and this is the HTML from the console

<p>the fraction </p><p><img ...></p>
 equals a natural number<p></p>

I would like that shortcodes would not be wrapped in p tags.

I know there is the plguin Toggle wpautop but since I write formulas in all pages, it would be a huge waste of time to manually write <p> tags in every page.


I searched a lot about the problem, and this is what I tried so far (all the following codes were placed in functions.php file)

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

add_shortcode( 'test', 'test_sc' );
function test_sc( $atts ){
    return apply_filters( 'the_content', '$\frac{15}{5} = 3$' );
}

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

add_shortcode( 'test', 'test_sc' );
function test_sc( $atts ){
    return wpex_clean_shortcodes(apply_filters( 'the_content','$\frac{15}{5} = 3$' ));
}

add_shortcode( 'test', 'test_sc' );
function test_sc( $atts ){
        $content = apply_filters( 'the_content','$\frac{15}{5} = 3$' );
        $content = shortcode_unautop($content);
        return $content;
}

add_shortcode( 'test', 'test_sc' );
function test_sc( $atts ){
        $content = apply_filters( 'the_content','$\frac{15}{5} = 3$' );
        return do_shortcode(wpautop($content));
}

and lastly, this code from Shortcode Empty Paragraph Fix plugin

add_shortcode( 'test', 'test_sc' );
function test_sc( $atts ){
        return apply_filters( 'the_content','$\frac{15}{5} = 3$' );
}

add_filter( 'the_content', 'shortcode_empty_paragraph_fix' );
function shortcode_empty_paragraph_fix( $content ) {
    // define your shortcodes to filter, '' filters all shortcodes
    $shortcodes = array( 'test' );
    foreach ( $shortcodes as $shortcode ) {
        $array = array (
            '<p>[' . $shortcode => '[' .$shortcode,
            '<p>[/' . $shortcode => '[/' .$shortcode,
            $shortcode . ']</p>' => $shortcode . ']',
            $shortcode . ']<br />' => $shortcode . ']'
        );
        $content = strtr( $content, $array );
    }
    return $content;
}

In all the previous cases, what gets displayed is

How to solve the problem?

I'm using a latex plugin to display formulas on my wordpress site, and I would like to add shortcodes with formulas inside them, so the content of the shortcodes must be processed by the plugin before it gets displayed in the page.

For example, by writing $\frac{15}{5} = 3$ in the editor, this gets displayed in the page .

So I wrote this simple shortcode

add_shortcode( 'test', 'test_sc' );
function test_sc( $atts ){
    return "$\frac{15}{5} = 3$";
}

but by writing [test] in the editor, this gets displayed in the page $\frac{15}{5} = 3$.

So I searched to solve the problem, and found out that by writing

add_shortcode( 'test', 'test_sc' );
function test_sc( $atts ){
    return apply_filters( 'the_content', '$\frac{15}{5} = 3$' );
}

the formula is displayed correctly, BUT it gets wrapped in <p> tags, so that by writing the fraction [test] equals a natural number, this gets displayed

and this is the HTML from the console

<p>the fraction </p><p><img ...></p>
 equals a natural number<p></p>

I would like that shortcodes would not be wrapped in p tags.

I know there is the plguin Toggle wpautop but since I write formulas in all pages, it would be a huge waste of time to manually write <p> tags in every page.


I searched a lot about the problem, and this is what I tried so far (all the following codes were placed in functions.php file)

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

add_shortcode( 'test', 'test_sc' );
function test_sc( $atts ){
    return apply_filters( 'the_content', '$\frac{15}{5} = 3$' );
}

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

add_shortcode( 'test', 'test_sc' );
function test_sc( $atts ){
    return wpex_clean_shortcodes(apply_filters( 'the_content','$\frac{15}{5} = 3$' ));
}

add_shortcode( 'test', 'test_sc' );
function test_sc( $atts ){
        $content = apply_filters( 'the_content','$\frac{15}{5} = 3$' );
        $content = shortcode_unautop($content);
        return $content;
}

add_shortcode( 'test', 'test_sc' );
function test_sc( $atts ){
        $content = apply_filters( 'the_content','$\frac{15}{5} = 3$' );
        return do_shortcode(wpautop($content));
}

and lastly, this code from Shortcode Empty Paragraph Fix plugin

add_shortcode( 'test', 'test_sc' );
function test_sc( $atts ){
        return apply_filters( 'the_content','$\frac{15}{5} = 3$' );
}

add_filter( 'the_content', 'shortcode_empty_paragraph_fix' );
function shortcode_empty_paragraph_fix( $content ) {
    // define your shortcodes to filter, '' filters all shortcodes
    $shortcodes = array( 'test' );
    foreach ( $shortcodes as $shortcode ) {
        $array = array (
            '<p>[' . $shortcode => '[' .$shortcode,
            '<p>[/' . $shortcode => '[/' .$shortcode,
            $shortcode . ']</p>' => $shortcode . ']',
            $shortcode . ']<br />' => $shortcode . ']'
        );
        $content = strtr( $content, $array );
    }
    return $content;
}

In all the previous cases, what gets displayed is

How to solve the problem?

Share Improve this question edited Sep 30, 2019 at 19:42 sound wave asked Sep 30, 2019 at 19:12 sound wavesound wave 2151 gold badge3 silver badges15 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Instead of trying to remove the automatically added paragraphs before or after the fact, you can try removing the wpautop filter from the_content within your shortcode (and re-adding it to maintain consistency for other plugins etc.):

add_shortcode( 'test', 'test_sc' );
function test_sc( $atts ){
    remove_filter( 'the_content', 'wpautop' );
    $content = apply_filters( 'the_content', '$\frac{15}{5} = 3$' );
    add_filter( 'the_content', 'wpautop' );
    return $content;
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745116230a4612143.html

相关推荐

  • plugins - Prevent shortcode from being wrapped in &lt;p&gt; tags

    I'm using a latex plugin to display formulas on my wordpress site, and I would like to add shortcodes with formulas

    10小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信