WordPress wp_localize_script : Variable PHP non reconnue par le script JS

Publié par Jean-Michel le

I am trying to modify a script (ajaxcalls.js) in my WordPress via my child theme. I created a duplicate of the file in the child theme (ajaxcalls_child.js).

I added the following code inside the function.php file in order to dequeue the old JS file, enqueue the new one and pass the variable ajaxcalls_vars which ajaxcalls_child.js uses:

<?php

//made sure here that the action execute after all the other scripts are loaded
add_action('wp_enqueue_scripts', 'wpestate_load_child_scripts', 100);

function wpestate_load_child_scripts() {
    wp_dequeue_script('wpestate_ajaxcalls');

    wp_enqueue_script('ajaxcalls_child', get_stylesheet_directory_uri().'/js/ajaxcalls_child.js', array('jquery'),'1.0', true);

    wp_localize_script(‘ajaxcalls_child’, ‘ajaxcalls_vars’, array(
        'contact_name'     =>  esc_html__( 'Your Name','wprentals'),
        'contact_email'    =>  esc_html__( 'Your Email','wprentals'),
        'contact_phone'    =>  esc_html__( 'Your Phone','wprentals'),
        'contact_comment'  =>  esc_html__( 'Your Message','wprentals'),
        'adv_contact_name' =>  esc_html__( 'Your Name','wprentals'),
        'adv_email'        =>  esc_html__( 'Your Email','wprentals'),
        'adv_phone'        =>  esc_html__( 'Your Phone','wprentals'),
        'adv_comment'      =>  esc_html__( 'Your Message','wprentals'),
        'adv_search'       =>  esc_html__( 'Send Message','wprentals'),
        'admin_url'        =>  get_admin_url(),
        'login_redirect'   =>  $login_redirect,
        'login_loading'    =>  esc_html__( 'Sending user info, please wait...','wprentals'),
        'userid'           =>  $userID,
        'prop_featured'    =>  esc_html__( 'Property is featured','wprentals'),
        'no_prop_featured' =>  esc_html__( 'You have used all the "Featured" listings in your package.','wprentals'),
        'favorite'         =>  esc_html__( 'Favorite','wprentals').'<i class="fas fa-heart"></i>',
        'add_favorite'     =>  esc_html__( 'Add to Favorites','wprentals'),
        'remove_favorite'  =>  esc_html__( 'remove from favorites','wprentals'),
        'add_favorite_unit'=>  esc_html__( 'add to favorites','wprentals'),
        'saving'           =>  esc_html__( 'saving..','wprentals'),
        'sending'          =>  esc_html__( 'sending message..','wprentals'),
        'reserve'          =>  esc_html__( 'Reserve Period','wprentals'),
        'paypal'           =>  esc_html__( 'Connecting to Paypal! Please wait...','wprentals'),
        'stripecancel'     =>  esc_html__( 'subscription will be cancelled at the end of the current period','wprentals'),
        'max_month_no'     =>  intval   ( get_option('wp_estate_month_no_show','') ),
        'processing'       =>  esc_html__( 'processing..','wprentals'),
        'home'             =>  get_site_url(),
        'delete_account'   =>  esc_html__('Confirm your ACCOUNT DELETION request! Clicking the button below will result your account data will be deleted. This means you will no longer be able to login to your account and access your account information: My Profile, My Reservations, My bookings, Invoices. This operation CAN NOT BE REVERSED!','wprentals'),
        'adv_search_what_half' =>  $adv_search_what_half,
        'adv_search_how_half'  =>  $adv_search_how_half,
        'adv_search_type'      =>  $adv_search_type
    ));
}

Now after I visit my website, I can see that the old script has been replaced by the new one but I can see on the chrome console that ajaxcalls_vars is not defined as soon as the a function needs it in the new script.

Error on the chrome console

Erreur sur la console chrome

Donc, le nouveau script est là mais ma variable ajaxcalls_varsn’est pas définie selon ajaxcalls_child.js , par conséquent, je pense qu’il y a quelque chose qui ne va pas dans la façon dont j’essaie de transmettre cette variable au nouveau script.

Après avoir examiné un problème similaire, j’ai compris un autre type d’erreur et j’ai essayé d’améliorer ce code jusqu’à celui que j’ai partagé ci-dessus. Mais ne peut toujours pas faire les choses correctement.

Solution n°1 trouvée

Vos guillemets simples autour des 2 premiers arguments wp_localize_script(‘ajaxcalls_child’, ‘ajaxcalls_vars’, array(...ne sont pas de vrais guillemets simples. Essayez de les supprimer et de les remplacer par le 'caractère wp_localize_script('ajaxcalls_child', 'ajaxcalls_vars', array(...
Une fois que j’ai fait cela, l’objet est disponible dans le script en file d’attente (testé sur xampp wp 5.0)

Catégories : Wordpress

Jean-Michel

Jean-Michel est en charge de la partie blog du site. Il met en place la stratégie de contenu et répond aux questions fréquentes sur Wordpress.

0 commentaire

Laisser un commentaire

Avatar placeholder

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *