X

How to Add Shortcode Support to WordPress Comments

Snippets by IsItWP

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).

Comments   Leave a Reply

  1. Will you consider making it as a plugin?

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!