Discussion:
wp_enqueue_script was called incorrectly...
BenderisGreat
2013-11-11 18:53:08 UTC
Permalink
I am trying to enqueue a script, and it's not working out so well for me.
Here is the code, places after the add_action for the function;

wp_enqueue_script('my_post_like_action', plugins_url().'/js/post-like.js',
array('jquery'), '1.0', true );
wp_localize_script('my_post_like_action', 'ajax_var', array(
'url' => admin_url('admin-ajax.php'),
'nonce' => wp_create_nonce('ajax-nonce')
));


I am receiving this error:
Notice: wp_enqueue_script was called incorrectly. Scripts and styles should
not be registered or enqueued until the wp_enqueue_scripts,
admin_enqueue_scripts, or login_enqueue_scripts hooks. Please see Debugging
in WordPress for more information. (This message was added in version 3.3.)
in /Users/Jared/Documents/Websites/www.local.dev/wp-includes/functions.php
on line 3060



--
View this message in context: http://wordpress-hackers.1065353.n5.nabble.com/wp-enqueue-script-was-called-incorrectly-tp42778.html
Sent from the Wordpress Hackers mailing list archive at Nabble.com.
Otto
2013-11-11 18:59:39 UTC
Permalink
Wrap your call to both of those in a function hooked to wp_enqueue_scripts.
Like this:

function test() {
// your code here
wp_enqueue_script('my_post_like_action', plugins_url().'/js/post-like.js',
array('jquery'), '1.0', true );
wp_localize_script('my_post_like_action', 'ajax_var', array(
'url' => admin_url('admin-ajax.php'),
'nonce' => wp_create_nonce('ajax-nonce')
));
// your code above
}
add_action('wp_enqueue_scripts','test');



-Otto


On Mon, Nov 11, 2013 at 12:53 PM, BenderisGreat
Post by BenderisGreat
I am trying to enqueue a script, and it's not working out so well for me.
Here is the code, places after the add_action for the function;
wp_enqueue_script('my_post_like_action', plugins_url().'/js/post-like.js',
array('jquery'), '1.0', true );
wp_localize_script('my_post_like_action', 'ajax_var', array(
'url' => admin_url('admin-ajax.php'),
'nonce' => wp_create_nonce('ajax-nonce')
));
Notice: wp_enqueue_script was called incorrectly. Scripts and styles should
not be registered or enqueued until the wp_enqueue_scripts,
admin_enqueue_scripts, or login_enqueue_scripts hooks. Please see Debugging
in WordPress for more information. (This message was added in version 3.3.)
in /Users/Jared/Documents/Websites/www.local.dev/wp-includes/functions.php
on line 3060
--
http://wordpress-hackers.1065353.n5.nabble.com/wp-enqueue-script-was-called-incorrectly-tp42778.html
Sent from the Wordpress Hackers mailing list archive at Nabble.com.
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
BenderisGreat
2013-11-11 19:43:44 UTC
Permalink
Thank you, that did it. So I have previously used the wp_create_nonce in the
item being targetted. For example, a submit button would have a data-nonce=
"<?php wp_create_nonce etc...

When using enqueue scripts, should I be creating the nonce there in that
function instead? I am not sure the purpose of that. Additionally, I am
following this tutorial:

http://wp.tutsplus.com/tutorials/how-to-create-a-simple-post-rating-system-with-wordpress-and-jquery/

And in step one, they create
and then proceed to use ajax to extrapolate the post ID without every
echoing the post_id in the button data attribute. How is this done? <#>



--
View this message in context: http://wordpress-hackers.1065353.n5.nabble.com/wp-enqueue-script-was-called-incorrectly-tp42778p42782.html
Sent from the Wordpress Hackers mailing list archive at Nabble.com.
Loading...