2

Is there an easy way to enqueue a JavaScript file with url_query values populated?

I have a php file underneath my js directory with JavaScript headers that I want to generate some dynamic JavaScript depending on the url_query values.

Thanks

Gs

UPDATE:

Yes, in the widget class I added the following (as a test):

(in the constructor, I just wanted to make a completely unique ID so I took the timestamp + a bunch of random #s):

$this->widget_id=time().rand(2,10).rand(2,10).rand(2,10).rand(2,10).rand(2,10).rand(2,10);
add_action('wp_footer', array( &$this, 'footer_js' ));

(then the method):

public function footer_js(){
      wp_register_script( 'mgs-site-script'.$this->widget_id, $this->plugin_location . "js/mgs.js", array('jquery'),'1.2.0');
      wp_enqueue_script( 'mgs-site-script'.$this->widget_id );
      $translation_array = array( 'some_string' => __( 'Some string to translate' ), 'a_value' => '10' );
      wp_localize_script( 'mgs-site-script', 'object_name', $translation_array );
  }

Then in mgs.js I tried to access the variables by calling:

alert(object_name.some_string);

but firebug spits out "object_name is not defined"

5
  • have you tried using wp_localize_script ? Its a easy way to passing variables/values to javascript file. Not via url parameter though. It creates a global object. codex.wordpress.org/Function_Reference/wp_localize_script
    – Sisir
    May 15, 2012 at 21:01
  • I tried it - but it didnt work, I updated the question w/ details May 17, 2012 at 14:49
  • If you're not hooking the wp_enqueue_script() function to the wp_enqueue_script hook, you're asking for trouble. It comes with an $in_footer parameter which it seems you'd take advantage of.
    – mrwweb
    May 17, 2012 at 15:01
  • Great call, that was oversight on my part. May 17, 2012 at 15:33
  • HOWEVER... it seems that I cannot enqueue a script from the "Widget" method in a widget object, is that correct? Can't find anything online about this. May 17, 2012 at 15:34

1 Answer 1

3

I can't get super specific without a more-detailed example from you, but I think you could combine get_query_var() and wp_enqueue_script() to do what you want like this:

add_action( 'wp_enqueue_scripts', 'sg_scripts' );
function sg_scripts() {

    $current_slug = get_query_var( 'page_name' );
    wp_enqueue_script( 'sg_custom_js', get_template_directory_uri() . '/js/somefile.php?slug=' . $current_slug, $dependencies, $version, $in_footer);

}

(I left the last three arguments as placeholders.)

Admittedly I've never tried this. A few things that might give you trouble:

  • I don't know what happens if wp_enqueue_script() is passed a non-.js file. It might get filtered out.
  • I know that a lot of the caching plugins (and WordPress?) look to the $version arg for caching. If you're seeing an old version of the js from a recent slug get served, I imagine that's what's going on.

Alternately, this is much less dynamic, but you can wrap individual wp_enqueue_script() instances with static .js files in if statements that test the query_var e.g.:

$current_slug = get_query_var( 'page_name' );
if( $current_slug == 'something' ) {
    \\ enqueue a script
} elseif( $current_slug == 'somethingelse' ) {
    \\ enqueue a different script
}
} else {
    \\ enqueue a third script
}
1
  • If this answer answers your question, you should accept it, by clicking the checkmark beneath the answer up/down vote arrows. Aug 20, 2012 at 15:46

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.