Home / comment / Add shortcode support to comments
  • How to Add shortcode support to comments

    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.

    ( click code to copy )

    wordpress snippet : PHP

    <>
    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');
    
    ( click code to copy )

    wordpress snippet : PHP -sample shortcode

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

    Will you consider making it as a plugin?