I want to add some javascript directly below content, added by another plugin's shortcode.

We are using the NextGen Gallery plugin and there are many articles containing the [nggallery] shortcode. I don't want to change all articles and I don't want to inform all authors to use another shortcode in the future. And I also don't want to modify the original plugin.

Is there a way to modify a shortcode like:

function put_script_after_nggallery_shortcode( $atts ){
 return "[nggallery $atts] <script>foo();</script>";
add_shortcode( 'nggallery', 'put_script_after_nggallery_shortcode' );

or should I hook somehow into the rendering process of the_content() to put my script directily in the_content string after [nggallery id=nnn]. Could that be done with a filter for the_content?

2 Answers 2


you could

remove_shortcode( 'nggallery' );

and then add it again:

function put_script_after_nggallery_shortcode( $atts ){
 global $nggShortcodes;
 $nggShortcodes->show_gallery($atts); //or otherwise duplicate the callback function
 wp_enqueue_script('special-ngg-sciprt', 'url-to-script', null, null, true);

add_shortcode( 'nggallery', 'put_script_after_nggallery_shortcode' );

i'm not sure my show_gallery will work, but seems logical

what script do you need to add? why not just wp_enqueue_script on its own? there isn't any reason that the script code has to be inline.


Since $nggShortcodes is apparently not a global variable, we can try using object notation... or you could just copy the contents of NextGen's gallery shortcode callback if this doesn't work. Also, I added a class check to ensure that this won't break your site if NextGen is ever deactivated.

function put_script_after_nggallery_shortcode( $atts ){
   if( ! class_exists('NextGEN_Shortcodes')) return;
   NextGEN_Shortcodes->show_gallery($atts); //or paste in the callback function

  <script type="text/javascript">
   jQuery(document).ready(function($) {


add_shortcode( 'nggallery', 'put_script_after_nggallery_shortcode' );
  • $nggShortcodes is no global variable.I want to have the script inline directly after the thumbnails of the gallery, because it adds onClick handlers to the thumbnails and between the thumbnails and the footers are too many social-media plugins that are evaluated before the script in the footer hook. So there is a pretty long time, where klicking on the thumb will do the wrong thing. Sep 10, 2012 at 10:52
  • Ok, well if it isn't global then perhaps you could use object notation. If you need the script directly then you could write it in the function instead of using wp_enqueue_script. Sep 10, 2012 at 16:54

Since WordPress 4.7 you can use the do_shortcode_tag filter.

add_filters( 'do_shortcode_tag', 'wpse_106269_do_shortcode_tag', 10, 4 );
function wpse_106269_do_shortcode_tag( $output, $tag, $attr, $m ) {
  if( $tag === something ) {
    //* Do something useful
  return $output;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.