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”
I can’t get super specific without a more-detailed example from you, but I think you could combine
get_query_var()
andwp_enqueue_script()
to do what you want like this:(I left the last three arguments as placeholders.)
Admittedly I’ve never tried this. A few things that might give you trouble:
wp_enqueue_script()
is passed a non-.js file. It might get filtered out.Alternately, this is much less dynamic, but you can wrap individual
wp_enqueue_script()
instances with static .js files inif
statements that test thequery_var
e.g.: