Run shortcode before filters

My users post snippets code in comments.
I created a shortcode for this :

function post_codigo($atts,$content=""){

        return '<code>'.$content.'</code>';
}

add_shortcode('codigo','post_codigo');  

Problem is that html gets filtered before its wrapped into the code tags.

Read More

I think that if i can get the shortcode run before filters then i can use

function pre_esc_html($content) {
  return preg_replace_callback(
    '#(<code.*?>)(.*?)(</code>)#imsu',
    create_function(
      '$i',
      'return $i[1].esc_html($i[2]).$i[3];'
    ),
    $content
  );
}

add_filter(
  'the_content',
  'pre_esc_html',
  9
);

that i found around here. What you think?

UPDATED: Ok now i changed my shortcode to:

 function post_codigo($atts,$content=""){

            return '<code>'.esc_html($content).'</code>';
    }

    add_shortcode('codigo','post_codigo');

But it still adding

breaks and breaking the whole code into several tags

Related posts

Leave a Reply

4 comments

  1. You can filter the array $no_texturize_shortcodes, which is a collection of shortcodes that are excluded from wptexturize. If you do this, everything within [codigo] shortcode tags will not be texturized:

    add_filter( 'no_texturize_shortcodes', 'no_texturize_codigo_shortcode' );
    
    function no_texturize_codigo_shortcode( $excluded_shortcodes ) {
        $excluded_shortcodes[] = 'codigo';
        return $excluded_shortcodes;
    }
    
  2. The no_texturize_shortcodes filter did nothing for me, as the content inside the shortcode would have already been filtered by wpautop() and any other filters hooked to the the_content. I resorted to changing the filter order of do_shortcode in relation to the_content, so that I can be certain it runs first of the two. Here’s how:

    remove_filter('the_content', 'do_shortcode', 11); // AFTER wpautop()
    add_filter('the_content', 'do_shortcode', 9); // BEFORE wpautop()
    

    This seems to work out fairly well, but as this is quite an extensive alteration to the way WordPress handles its text/html conversion I’d recommend you use it with caution. The more plugins/shortcodes you are using the more likely you are to run in to trouble relating to this change I’d guess.

  3. Is your theme using comment_text() to display your visitor’s comments?

    if so try changing:

    <?php comment_text(); ?>
    

    to:

    <?php echo do_shortcode(apply_filters('comment_text', get_comment_text())); ?>
    

    This will apply any “comment_text” filters to the comment text, before it will apply the shortcode.

    This should be done in your theme templates wherever comments are displayed.