0

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?

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;
}
  • Thank you very much so simple! – sound wave Oct 1 '19 at 6:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.