admin管理员组文章数量:1122832
Iam having problems enqueueing a script with wp_localize_script. I have done this several times before and I cannot see what Iam doing wrong here.
I have changed the handle of the script several times and looked at working code in other plugins, but I cannot find the error.
I register my script like this:
add_action( 'wp_enqueue_scripts', array( $this, 'frontend_enqueue' ) );
function frontend_enqueue() {
wp_register_script( 'my-handle', plugins_url( 'js/frontend.js', __FILE__), array( 'jquery' ), '1.0.0', true);
}
I want to enqueue the script in a shortcode function:
add_shortcode( 'my_shortcode', array( $this, 'shortcode_template' ) );
function shortcode_template( $atts ) {
...
// Ajax and custom scripts
wp_localize_script('my-handle', 'ajax_actions', array(
'ajaxurl' => admin_url( 'admin-ajax.php' )
));
wp_enqueue_script( 'my-handle' );
...
}
The file frontend.js get enqueued but I always get "not defined" error. The error is pointing to this line:
var ajaxUrl = ajax_actions.ajaxurl;
jquery.min.js?ver=3.7.1:2 jQuery.Deferred exception: ajax_actions is not defined
Iam having problems enqueueing a script with wp_localize_script. I have done this several times before and I cannot see what Iam doing wrong here.
I have changed the handle of the script several times and looked at working code in other plugins, but I cannot find the error.
I register my script like this:
add_action( 'wp_enqueue_scripts', array( $this, 'frontend_enqueue' ) );
function frontend_enqueue() {
wp_register_script( 'my-handle', plugins_url( 'js/frontend.js', __FILE__), array( 'jquery' ), '1.0.0', true);
}
I want to enqueue the script in a shortcode function:
add_shortcode( 'my_shortcode', array( $this, 'shortcode_template' ) );
function shortcode_template( $atts ) {
...
// Ajax and custom scripts
wp_localize_script('my-handle', 'ajax_actions', array(
'ajaxurl' => admin_url( 'admin-ajax.php' )
));
wp_enqueue_script( 'my-handle' );
...
}
The file frontend.js get enqueued but I always get "not defined" error. The error is pointing to this line:
var ajaxUrl = ajax_actions.ajaxurl;
jquery.min.js?ver=3.7.1:2 jQuery.Deferred exception: ajax_actions is not defined
Share
Improve this question
asked Jul 30, 2024 at 10:43
LWS-MoLWS-Mo
1,5721 gold badge10 silver badges16 bronze badges
2
|
1 Answer
Reset to default 0I think it's because you're doing your localization in the shortcode which may be messing with the order of things.
Instead do this:
add_action( 'wp_enqueue_scripts', array( $this, 'frontend_enqueue' ) );
function frontend_enqueue() {
wp_register_script( 'my-handle', plugins_url( 'js/frontend.js', __FILE__), array( 'jquery' ), '1.0.0', true);
wp_localize_script('my-handle', 'ajax_actions', array(
'ajaxurl' => admin_url( 'admin-ajax.php' )
));
}
Then...
add_shortcode( 'my_shortcode', array( $this, 'shortcode_template' ) );
function shortcode_template( $atts ) {
...
// Ajax and custom scripts
wp_enqueue_script( 'my-handle' );
...
}
I think the script is being registered and the enqueuing fires when your shortcode loads but the script was already registered so the localization isn't present and that means the enqueued script doesn't have the object. I've never seen the localization done during output.
本文标签: ajaxObject name undefined using localize script
版权声明:本文标题:ajax - Object name undefined using localize script 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736299185a1930395.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
$in_footer
parameter, but Iam already using in footer true withwp_register_script
. Maybe Iam not understanding correctly? – LWS-Mo Commented Aug 1, 2024 at 7:37