3

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.

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

4 Answers 4

4
+50

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;
}
4
  • 1
    I tried this but im getting same result. If you check source of masquewordpress.com/ayuda/… you will see that javascript is still as javascript. I need to scape all text in my shortcode function?
    – chifliiiii
    Nov 7, 2011 at 21:13
  • I escaped my shortcode now but still acting funny. I updated my question
    – chifliiiii
    Nov 7, 2011 at 21:21
  • hmmm. I don't really see it. It seems to look OK to me. Are you still having issues with it? Nov 7, 2011 at 22:11
  • Nope, i only changed the <code> tag to <pre> and the problem solved. Thanks for the help
    – chifliiiii
    Nov 8, 2011 at 22:05
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.

0

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.

1
  • Thanks but i need the other way around. The shortcode should run before filter so then i can avoid filtering inside the shortcode.
    – chifliiiii
    Nov 7, 2011 at 11:53
0

You should to use html chars?

return '&lt;code&gt;'.$content.'&lt;/code&gt;';

.
Tried this localy and it works gr8

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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