Do you want to add shortcode support to WordPress Comments? With our snippet, you can whitelist some shortcodes that you want to enable on WordPress comments.
Instructions:
All you have to do is add these code snippets to your theme’s functions.php file or in a site-specific plugin:
The first snippet removes all shortcodes and reinitializes only those listed within init_common_shortcodes function. The second sample snippet replaces the [mysite] shortcode with the user’s website URL.
Snippet #1
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');
Snippet #2
function mysite(){ return '<a href="' .get_comment_author_url(). '">(My website)</a>'; } add_shortcode('mysite', 'mysite');
Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly copy / paste code snippets in WordPress, so you don’t accidentally break your site.
If you liked this code snippet, please consider checking out our other articles on the site like: Best WordPress Page Builders (Compared).
Will you consider making it as a plugin?