• Hi,
    I think I’m doing something wrong here, not sure what tho.
    So, I have followed your instruction on this forum post, codex.. everywhere (by the way thanks guys you’re an awesome resource) and I manage to write down this code for including some scripts in the footer.

    The function works fine, but my site is bilingual so I create 2 function with the condition if(is_page( ‘slug’ ) ) add_action xx else add-action yy, where the slug ‘homepage’ is the english version of the page.

    For some reasons the conditional results always false so the script loaded is never in english.

    I tested the function with other slugs, same problem. Could this happening because I’m using Polylang to translate the website?

    With this plugin my url changes from site_url/page_slug/ to site_url/en/page_slug/

    The plugin documentation https://polylang.wordpress.com/documentation/

    Here is my function:

    <?php
    // Italian scripts
    function be_script_it() {
    wp_register_script( 'booking_language_it', get_template_directory_uri() . '/globalize/globalize-it.js', array( 'jquery' ), null , true);
    wp_register_script( 'booking_searchbox_it',  'https://be.bookingexpert.it/book/websites/searchbox?hotel=5098&lang=it', array (), null , true);
    wp_register_script( 'booking_modulo', get_template_directory_uri() . '/globalize/searchbox.js', array (), null , true );
    
    wp_enqueue_script('booking_language_it');
    wp_enqueue_script('booking_searchbox_it');
    wp_enqueue_script('booking_modulo');
    }
    
    // English scripts
    function be_script_en() {
    wp_register_script( 'booking_language_en', get_template_directory_uri() . '/globalize/globalize-en.js', array( 'jquery' ), null , true);
    wp_register_script( 'booking_searchbox_en',  'https://be.bookingexpert.it/book/websites/searchbox?hotel=5098&lang=en', array (), null , true);
    wp_register_script( 'booking_modulo', get_template_directory_uri() . '/globalize/searchbox.js', array (), null , true );
    
    wp_enqueue_script('booking_language_en');
    wp_enqueue_script('booking_searchbox_en');
    wp_enqueue_script('booking_modulo');
    }
    
    // conditions if the slug page is homepage (en version page)
    if(is_page( 'homepage' ) ) add_action('wp_enqueue_scripts', 'be_script_en');
    else add_action('wp_enqueue_scripts', 'be_script_it');
    
    ?>

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter Sybyll

    (@sybyll)

    I just read this article about conditional tags in function. Is it right? Should I put the conditional inside the function?

    http://www.organizedthemes.com/loading-scripts-conditionally/

    Thread Starter Sybyll

    (@sybyll)

    I found the solution thanks to the link above.
    Is someone may interest in it, I post the right code below

    // I register all my script in 1 function first
    function organizedthemes_load_default_scripts() {
        if( !is_admin()){
    wp_register_script( 'booking_language_it', get_template_directory_uri() . '/globalize/globalize-it.js', array( 'jquery' ), null , true);
    wp_register_script( 'booking_searchbox_it',  'https://be.bookingexpert.it/book/websites/searchbox?hotel=5098&lang=it', array (), null , true);
    wp_register_script( 'booking_modulo', get_template_directory_uri() . '/globalize/searchbox.js', array (), null , true );
    wp_register_script( 'booking_language_en', get_template_directory_uri() . '/globalize/globalize-en.js', array( 'jquery' ), null , true);
    wp_register_script( 'booking_searchbox_en',  'https://be.bookingexpert.it/book/websites/searchbox?hotel=5098&lang=en', array (), null , true);
    		 }
    }
    add_action('wp_enqueue_scripts', 'organizedthemes_load_default_scripts');
    
    // function with  coditions
    function be_script_it() {
    
    // if the page is called/ with slug 'home' the load xx script
    if(is_page( 'home' ) ){
    wp_enqueue_script('booking_language_it');
    wp_enqueue_script('booking_searchbox_it');
    wp_enqueue_script('booking_modulo');
    }
    // if is not true then load yy script
    else{
    wp_enqueue_script('booking_language_en');
    wp_enqueue_script('booking_searchbox_en');
    wp_enqueue_script('booking_modulo');
    }
    }
    add_action('wp_enqueue_scripts', 'be_script_it');

    As is now the code load the second part of the script in all pages except for ‘home’. In my case I need the scripts in just 2 pages (en+it) and 4 post (en+it), so I changed the second part of my function like this

    function be_script_it() {
    // if is this specific IT page title and this IT posts title then load IT script
    if(is_page( 'home' )|| is_single( array( 'camera-deluxe', 'camera-classica' ) )  ){
    wp_enqueue_script('booking_language_it');
    wp_enqueue_script('booking_searchbox_it');
    wp_enqueue_script('booking_modulo');
    }
    // if is this specific EN page title and this EN posts title then load EN script
    if(is_page( 'homepage' )|| is_single( array( 'deluxe-room', 'classic-room' ) )  ){
    wp_enqueue_script('booking_language_en');
    wp_enqueue_script('booking_searchbox_en');
    wp_enqueue_script('booking_modulo');
    }
    
    // if both cases aren't true then load nothing
    else{}
    }
    add_action('wp_enqueue_scripts', 'be_script_it');
Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Problem loading script files by page’ is closed to new replies.