• Resolved erikspen

    (@erikspen)


    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://ww.wp.xz.cn/plugins/shortcodes-ui/

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter erikspen

    (@erikspen)

    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

    (@bainternet)

    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.