1

Here i followed the solution here to localize script : Problem in wp_localize_script

But its not working.

Below is my codes:

function theme_enqueue_scripts()
{
    wp_enqueue_script( array("jquery") );

    wp_enqueue_script('thebootstrap', get_template_directory_uri() . '/bootstrap/js/bootstrap.min.js',         

    wp_localize_script('reservation-scripts', 'admin_ajax', array(
        'ajaxurl' => esc_url(admin_url('admin-ajax.php'))
    ));
    wp_enqueue_script( 'reservation-scripts' );
    /*
    * Scripts for IE9
    */
    wp_enqueue_script('thehtml5shiv', '//oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js');
    wp_script_add_data('thehtml5shiv', 'conditional', 'lt IE 9');
    wp_enqueue_script('therespond', '//oss.maxcdn.com/respond/1.4.2/respond.min.js');
    wp_script_add_data('therespond', 'conditional', 'lt IE 9');
}
add_action('wp_enqueue_scripts', 'theme_enqueue_scripts');

1 Answer 1

1

There are a few things wrong...the most pertinent of which is that the script that you're going to localize with wp_localize_script() must be registered before you call wp_localize_script().

Secondly, your call to wp_enqueue_script( 'reservation-scripts' ) doesn't provide the source (i.e., URL) for the script.

Lastly, they way you are enqueueing jQuery is incorrect.

So, if you change your code to the following you'll be fine:

function theme_enqueue_scripts()
{
    wp_enqueue_script('thebootstrap',
       get_template_directory_uri() . '/bootstrap/js/bootstrap.min.js', array('jquery') ) ;

    // be sure to substitute the actual URL for
    // your script in URL_FOR_YOUR_SCRIPT
    wp_register_script( 'reservation-scripts', URL_FOR_YOUR_SCRIPT );
    wp_localize_script('reservation-scripts', 'admin_ajax', array(
        'ajaxurl' => esc_url(admin_url('admin-ajax.php'))
    ));
    wp_enqueue_script( 'reservation-scripts' );
    /*
     * Scripts for IE9
     */
    wp_enqueue_script('thehtml5shiv', '//oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js');
    wp_script_add_data('thehtml5shiv', 'conditional', 'lt IE 9');
    wp_enqueue_script('therespond', '//oss.maxcdn.com/respond/1.4.2/respond.min.js');
    wp_script_add_data('therespond', 'conditional', 'lt IE 9');
}
add_action('wp_enqueue_scripts', 'theme_enqueue_scripts');
3
  • thanks a lot.Btw i don't understand URL_FOR_YOUR_SCRIPT .which url?
    – user117128
    Commented Apr 15, 2017 at 16:20
  • it would be something like get_template_directory_uri() . 'js/your_script.js' Commented Apr 15, 2017 at 16:25
  • okay but is it necessary to localize admin-ajax?
    – user117128
    Commented Apr 15, 2017 at 17:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.