• Resolved giannit

    (@giannit)


    I would like to add shortcodes whose content gets processed by plugins before it gets displayed in the page.

    In particular, I have a plugin which processes content between two $ signs to produce math formulas and graphs.
    A simple shortcode is

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

    By writing $\frac{15}{5} = 3$ in a page this gets displayed

    By writing [test] in a page this gets displayed $\frac{15}{5} = 3$

    WordPress does a few things before the content from a page or post gets displayed on the site. For instance, it processes HTML paragraph (p) tags, it runs shortcodes and even sends the content to the theme and any plugins so they can do their thing to the content and include their bits.

    I heard that using do_shortcode_tag (reference) it is possibile to do full content processing, but how to do that?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter giannit

    (@giannit)

    I found out that by using the code

    return apply_filters( 'the_content', '$\frac{15}{5} = 3$' );

    the formula is showed correctly, but as side effect it is displayed inside <p> tags, and so a sentence such as

    this is [test] a fraction

    gets displayed in 3 lines (the first one contains this is, the second one contains the formula, the third one contains a fraction).
    Is it possible to show it without the p tags?

    I would like to add shortcodes whose content gets processed by plugins before it gets displayed in the page.

    I think you have it backwards.
    The plugin is doing its own parsing and not using the shortcode system. By writing a shortcode, you can force it to be filtered by the shortcode system, but it won’t be all of the content.

    I heard that using do_shortcode_tag (reference) it is possibile to do full content processing

    That filter applies to the content of a shortcode, not the full content.

    You can make a shortcode that wraps the plugin call, so that the shortcdoe filters are available. See the parameters passed to the shortcode function, here.

    Thread Starter giannit

    (@giannit)

    Thank you @joyously for reply.
    Using the code return apply_filters( 'the_content', '$\frac{15}{5} = 3$' ); the content is displayed correctyle, anyway it is wrapped inside <p> tags.
    I’m trying to avoid this but none of the following worked, you have any suggestion?

    What I tried so far

    1. remove_filter( 'the_content', 'wpautop' );
      add_filter( 'the_content', 'wpautop' , 99);
      add_filter( 'the_content', 'shortcode_unautop',100 );
    2. function wpex_clean_shortcodes($content){   
      $array = array (
          '<p>[' => '[', 
          ']</p>' => ']', 
          ']<br />' => ']'
      );
      $content = strtr($content, $array);
      return $content;
      }
      add_filter('the_content', 'wpex_clean_shortcodes');
    3. $content = '<div class="two-column">'.$content.'</div>';
      $content = shortcode_unautop($content);
      return $content;
    4. return do_shortcode(wpautop($content));

    5. Shortcode Empty Paragraph Fix plugin
    • This reply was modified 6 years, 4 months ago by giannit.
    Thread Starter giannit

    (@giannit)

    This is what I tried so far in details (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));
    }

    ————————–

    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

    and the HTML is

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

    I would like that the shortcode would not be wrapped in p tags, how to get rid of them?

    Thread Starter giannit

    (@giannit)

    Instead of trying to remove the automatically added paragraphs before or after the fact, the solution is to remove the wpautop filter from the_content within the 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;
    }
Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘How to use do_shortcode_tag to modify the output of a shortcode?’ is closed to new replies.