Ich habe den folgenden Code in meinem Thema functions.php
, aber wenn ich console.log(pw_script_vars);
aufrufen, ist die Variable undefined
. Was habe ich verpasst?wp_localize_script funktioniert nicht
function mytheme_enqueue_scripts(){
wp_enqueue_script('jquery');
}
add_action('wp_enqueue_scripts', 'mytheme_enqueue_scripts');
function pw_load_scripts() {
wp_enqueue_script('pw-script');
wp_localize_script('pw-script', 'pw_script_vars', array(
'alert' => __('Hey! You have clicked the button!', 'pippin'),
'message' => __('You have clicked the other button. Good job!', 'pippin')
)
);
}
add_action('wp_enqueue_scripts', 'pw_load_scripts');
Vielen Dank für Ihre Antwort, aber meine Absicht war, einige PHP-Arrays in Jquery übergeben, wo ich auf jeder Seite meiner Vorlage ansprechen kann ... –
Das ist genau das, was der Code oben getan hat. Hattest du Probleme? – RRikesh
Ich habe das Skript von: http://papermashup.com/jquery-iphone-style-ajax-switch/ und ich machte: \t wp_enqueue_script ('ajax-switch', get_bloginfo ('template_url'). '/ Includes/ajaxswitch /jquery.iphone-switch.js ', false, false); \t global $ current_user; \t wp_localize_script ('ajax-Switch', 'ajax_switch', array ( \t \t \t \t 'post_status' => get_user_meta ($ current_user-> ID '_fbpost_status', true), \t \t \t \t 'templateUrl' = .> get_template_directory_uri() '/ includes/ajaxswitch /' \t \t \t \t) \t \t); Und arbeiten Vielen Dank, die zweite Sache ist, dass in diesen PHP-Dateien muss ich die Wordpress-Variablen und PHP-Bibliothek enthalten. Wie? –