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'm working on a plugin which uses a shortcode to call a JQuery function, and it requires three aspects to work:

  1. The JQuery code + its own code, to be included on the pages it will be used on
  2. The HTML element it will work on requires an "id" attribute, in order to reference it
  3. A reference/initialization in a $(document).ready(function()) block

I've already implemented 1 and 2; 1 by using wp_register_script and, if the shortcode appears, wp_print_scripts to print it; and 2 by making "id" an attribute of my shortcode, which also gets returned, e.g. [myshortcode id="foo"] returns id="foo".

The problem is 3. The initialization is of the form:

$(document).ready(function() 
    { 
        $("#id").func( {attributes} ); 
     } 
 );

I've considered using wp_localize_script over a variable for the ID; however, I do not know how many times the shortcode will appear on a given page, and it seems sloppy to register an arbitrary number of otherwise-identical scripts and just hope that the number won't be surpassed.

This seems like it's probably pretty common, but I haven't been able to find anything related by searching. Any ideas? Thanks!

share|improve this question
    
So, you will have an html like <div id="f1"></div><div id="f2"></div>, and you want to execute $("#f1, #f2").something() in the footer? –  Dogbert Oct 17 '12 at 10:02
    
Yes, exactly. And so far, the way that I've implemented that is, e.g. <div [shortcode id="f1"]></table>. –  Jonathan Oct 17 '12 at 11:14

1 Answer 1

up vote 0 down vote accepted

For 2nd, i recommend using the php function uniqid(), takes some of the work of user.

For 3rd i think the best way to implement is to keep adding the id's after each shortcode call to a global/static variable in an array & then hook a function to wp_footer that reads from this variable to get all the id's & generate the neccessary javascript

For 1st, add this inside the shortcode function-

STATIC $flag = false;
if(!$flag) {
    $flag = true;

    // include all your scripts that you want to include in the page only once
    // These statements will be executed once per page load
    // irrespective of how many times shortcode is called.
}
share|improve this answer
    
For 3, my understanding of WordPress is that the Right Way (tm) to insert JavaScript is using WordPress functions (i.e. wp_enqueue_script/wp_register_script) rather than, e.g. echoing it into the page. The problem with using a WordPress function is that the `wp_enqueue_script' call needs to be done before the content is loaded, so it's impossible to know how many shortcodes and what their IDs are when making that call. –  Jonathan Oct 17 '12 at 11:12
    
you're right, the only way to add shortcode specific scripts is to echo it directly inside the shortcode content or hook it to wp_footer. You'll also need to change the priority in wp_footer manually so as to handle dependancies –  Mridul Aggarwal Oct 17 '12 at 11:21
    
That settles it then; thanks! –  Jonathan Oct 17 '12 at 12:39

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.