Title: Shortcodes in comments
Last modified: August 22, 2016

---

# Shortcodes in comments

 *  Resolved [erikspen](https://wordpress.org/support/users/erikspen/)
 * (@erikspen)
 * [11 years, 9 months ago](https://wordpress.org/support/topic/shortcodes-in-comments-1/)
 * I see that I can use a simple hook with do_shortcode to add my shortcodes to 
   user comments. This is great. However, not with external files. They don’t get
   loaded.
 * (Clearly, this is due to “conditionally_add_scripts_and_styles” function in “
   shortcode-maker.php” only looking at the post content.)
 * I’m guessing there’s not a super-easy work-around, but if someone knows of one,
   please share.
 * On a buggy side note…
    I noticed that you pass the $key to wp_enqueue_script,
   which ends up preventing loading more than one javascript file per shortcode.
   Ever think about changing $key to something else? Perhaps the path to the javascript
   file?
 * [https://wordpress.org/plugins/shortcodes-ui/](https://wordpress.org/plugins/shortcodes-ui/)

Viewing 2 replies - 1 through 2 (of 2 total)

 *  Thread Starter [erikspen](https://wordpress.org/support/users/erikspen/)
 * (@erikspen)
 * [11 years, 9 months ago](https://wordpress.org/support/topic/shortcodes-in-comments-1/#post-5272443)
 * Well, sort of solved the problem with this little hack in “_shortcode-maker.php_“.
 * First of all, any scripts that may appear in the comments you’ll have to include
   them as a **body tag**, since comments aren’t loaded until the end.
 * First, I added this function to check for shortcode tags in the comments:
 *     ```
       public function conditionally_add_scripts_and_styles_to_comments($comment){
       	if (empty($comment)) return $comment;
   
       	if (!isset($this->sc_tags) && !is_array($this->sc_tags))
       		return $comment;
   
       	$tags = array_keys($this->sc_tags);
   
       	foreach ($tags as $index => $t){
       		if (stripos($comment, '['.$t) !== false) {
       			$this->sc_external[$t]['found'] = true;
       		}
       	}
       	return $comment;
       }
       ```
   
 * Which I call around line 191 as follows:
 *     ```
       add_filter('comments_template', function(){ add_filter('comment_text', array($this, 'conditionally_add_scripts_and_styles_to_comments')); });
       add_filter('comments_template', function(){add_filter('comment_text', 'do_shortcode');});
       ```
   
 * This will work for both scripts and styles (as long as you specify them as being
   included as body tags), but probably won’t work for other advanced functions.
 *  Plugin Author [Bainternet](https://wordpress.org/support/users/bainternet/)
 * (@bainternet)
 * [11 years, 9 months ago](https://wordpress.org/support/topic/shortcodes-in-comments-1/#post-5272607)
 * Thats a nice way to do it.

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Shortcodes in comments’ is closed to new replies.

 * ![](https://s.w.org/plugins/geopattern-icon/shortcodes-ui_dbeef6.svg)
 * [ShortCodes UI](https://wordpress.org/plugins/shortcodes-ui/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/shortcodes-ui/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/shortcodes-ui/)
 * [Active Topics](https://wordpress.org/support/plugin/shortcodes-ui/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/shortcodes-ui/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/shortcodes-ui/reviews/)

 * 2 replies
 * 2 participants
 * Last reply from: [Bainternet](https://wordpress.org/support/users/bainternet/)
 * Last activity: [11 years, 9 months ago](https://wordpress.org/support/topic/shortcodes-in-comments-1/#post-5272607)
 * Status: resolved