Add shortcode support to comments

Adding this snippet to the functions.php of your wordpress theme will enable shortcodes for comments. This code will first remove all shortcodes when it gets to comments and reinitialize only the shortcodes listed within init_common_shortcodes function. I have added in a simple sample shortcode that will grab the users website url and replace the [mysite] shortcode.

 

function init_common_shortcodes() {
// add each shortcode you want enabled in comments
add_shortcode('mysite', 'mysite');
}
function init_comment_shortcodes() {
remove_all_shortcodes();
init_common_shortcodes();
add_filter('comment_text', 'do_shortcode');
}
init_common_shortcodes();
add_filter('comments_template', 'init_comment_shortcodes');

function mysite(){
return '<a href="' .get_comment_author_url(). '">(My website)</a>';
}
add_shortcode('mysite', 'mysite');

Be Sociable, Share!

Leave a comment