1

I have the situation that I need to include special scripts only in case a parameter in a macro is used. For the background, see the following:

  • My plugin ChessTempoViewer which wraps a JavaScript library for usage in WP.
  • There I add the necessary libraries by using wp_enqueue_script.
  • I would like to internationalize the output of the viewer, which is described here.

So to use that internationalization, I should have to include the relevant script depending on the locale that should be used. This should be of course defined by the user, so the user should have the option to do something like:

[ctlocale locale="de_DE"][/ctlocale]
[ctpgn] 1. e4 e5 2. Nf3 ... [/ctpgn]

which should result in an output for the moves:

1. e4 e5 2. Sf3

S for Springer in German instead of N for Night in English.

My code currently looks like that:

function pgnviewer_js_and_css(){
  wp_enqueue_script("jquery");
  wp_enqueue_script('pgnyui', 'http://chesstempo.com/js/pgnyui.js');
  wp_enqueue_script('pgnviewer', 'http://chesstempo.com/js/pgnviewer.js');
  wp_enqueue_style('pgnviewer-css', 'http://chesstempo.com/css/board-min.css');
  wp_enqueue_style('ctpgn', plugins_url('ctpgn.css', __FILE__));
}

add_action('wp_enqueue_scripts', 'pgnviewer_js_and_css');

function chessTempoViewer($attributes, $content = NULL) {
  ..
}

How could I include an additional tag ctlocale as in the example above (as the first entry) to ensure that the script is included before the other scripts?

1 Answer 1

1

Shortcodes are inconvenient for such things, because they are processed in content, way after site's header. However since you don't need to deal with styles, only a script, it's a little easier by moving stuff to footer.

Your logic would be something like following:

  1. Locale shortcode stores requested locale in some way.
  2. At some point between content and footer you register and enqueue scripts, making locale script dependency of main script.
  3. WP reaches footer, resolved enqueued scripts, and prints them out for you.
3
  • I only understand half of what you are telling me (sorry, first plugin and no experience in WordPress). Is there a convenient source to read the normal flow of control, so that I understand the options I have? And of course, thank's a lot for your patience ... Commented Apr 20, 2014 at 16:46
  • @mliebelt no worries, this takes some initial effort to grok :) See wordpress.stackexchange.com/a/21579/847 In your case wp_enqueue_scripts hook is too early (because shortcode hadn't run yet), so you want to do that later (but before wp_print_footer_scripts()). Commented Apr 20, 2014 at 16:52
  • I had a look at a possible solution, and decided, that it may work this way. I will implement a much simpler solution, btw by using the standard locale, and provide a setting for the plugin to set the locale for Chess Tempo Viewer. This will do what most people need, to use the same locale for all their (chess) pages. Thank's a lot for the help, I will come back and search for hints. I love stackoverflow ... Commented Apr 21, 2014 at 15:42

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.