0

I've bundled jquery in my theme js using grunt-uglify. Now I want to prevent wp jquery from loading since I don't need 2 jquery versions and want to save unnecessary requests. This is what I have:

add_action('wp_enqueue_scripts', function () {
    wp_dequeue_script('jquery');
    wp_deregister_script('jquery');
    wp_register_script('jquery', get_stylesheet_directory_uri() . '/build/scripts/app.js');
    wp_enqueue_script('jquery');
});

add_action('wp_enqueue_scripts', function () {
    global $wp_query;
    wp_localize_script('jquery', 'ajaxpagination', array(
        'ajaxurl'      => admin_url('admin-ajax.php'),
        'query_vars'   => json_encode($wp_query->query),
        'is_search'    => is_search(),
        'current_page' => get_query_var('paged') ? get_query_var('paged') : 1,
        'max_page'     => $wp_query->max_num_pages,
    ));
});

I named the $handle of wp_register_script jquery aswell so the dependencies will load. But for some reason, the CDATA/script tag from wp_localize_script will not be rendered once I register my script as jquery.

I've already tried

  • enqueueing without registering it first
  • deregistering jquery without dequeuing it first
  • May you try to add priority in your add_action ? – Elex Jun 20 '18 at 14:06
  • Why are you changing jquery to be your script? Just give your script its own name and enqueue/localize that. If plugin compatibility is a concern then you shouldn't be bundling your own copy. – Jacob Peattie Jun 20 '18 at 14:07
  • @JacobPeattie as stated, I want to save requests so I bundle jquery together with my theme js. So the theme js basically is jquery – Tom M Jun 20 '18 at 14:45
  • @Jean-RémiLARCELET-PROST adding a priority in add_action sadly does not change the outcome – Tom M Jun 20 '18 at 14:48
  • There are plugins for minifying and combining scripts and styles, I'd suggest you only include code that's yours and minify with a plugin. – Jacob Peattie Jun 20 '18 at 14:50
0

You can't use wp_localize_script with jquery. this function use the WP_Scripts::localize() function which have a condition right in the start.

You can see it in the file \wp-includes\class.wp-scripts.php line 414

if ( $handle === 'jquery' )
    $handle = 'jquery-core';

You can do something else like add the script with other action like wp_head if your script is in the <head> or wp_print_footer_scripts if its in the footer.

add_action('wp_enqueue_scripts', function () {
    wp_dequeue_script('jquery');
    wp_deregister_script('jquery');
    wp_register_script('jquery', get_stylesheet_directory_uri() . '/build/scripts/app.js');
    wp_enqueue_script('jquery');
});

add_action('wp_head', function() {
    global $wp_query;

    $my_object = array(
        'ajaxurl'      => admin_url('admin-ajax.php'),
        'query_vars'   => json_encode($wp_query->query),
        'is_search'    => is_search(),
        'current_page' => get_query_var('paged') ? get_query_var('paged') : 1,
        'max_page'     => $wp_query->max_num_pages,
    );
    ?>
    <script>
        var ajaxpagination = <?php echo wp_json_encode($my_object); ?>
    </script>
    <?php
}, 0, 1);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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