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
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:The
no_texturize_shortcodes
filter did nothing for me, as the content inside the shortcode would have already been filtered bywpautop()
and any other filters hooked to thethe_content
. I resorted to changing the filter order ofdo_shortcode
in relation tothe_content
, so that I can be certain it runs first of the two. Here’s how: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.
Is your theme using
comment_text()
to display your visitor’s comments?if so try changing:
to:
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.
You should to use html chars?
.
Tried this localy and it works gr8