Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer 1

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.

EDIT #1

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($) {
     alert('bacon');
   });

   </script>

 <?php
}
add_shortcode( 'nggallery', 'put_script_after_nggallery_shortcode' );
share|improve this answer
    
$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. –  Pascal Rosin Sep 10 '12 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. –  helgatheviking Sep 10 '12 at 16:54
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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