Use wc_enqueue_js only on specific pages – nested add_action

No idea why, but for the second snippet, changing the priority of the
nested add_action from the default (10) to 20, causing the parent
and child add_action to have a different priority solved the problem
and now everything works perfectly: add_action( 'template_redirect',
function() use ( $args ) { acau_enqueue_script ( $args ); }, 20);

The reason is quite simple. Because WordPress supports nested hooks, which means you can add another callback to the same/parent hook (e.g. template_redirect in your case), and the additional callback would be called if the priority is greater than the parent callback’s priority.

  • Case 1: In this case, only the some_func2() that would be executed. Because its priority is 12, which is greater than the parent callback (i.e. the closure).

    add_action( 'template_redirect', function(){
        add_action( 'template_redirect', 'some_func', 11 );  // first child hook
        add_action( 'template_redirect', 'some_func2', 12 ); // second child hook
    }, 11 ); // parent hook
    
  • Case 2: In this case, both some_func() and some_func2() would be executed. Because their priority is, once again, greater than the parent callback’s.

    add_action( 'template_redirect', function(){
        add_action( 'template_redirect', 'some_func', 11 );  // first child hook
        add_action( 'template_redirect', 'some_func2', 12 ); // second child hook
    } ); // parent hook with the default priority (10)
    

So when registering nested hooks, make sure the callback’s priority is greater than the parent’s.

Alternatives to using nested hooks

So this is specific to the original question about wc_enqueue_js() only on the cart page.

You can try one of these, which worked well for me:

  • Hook to wp, then hook acau_enqueue_script() to template_redirect:

    add_action( 'wp', function(){
        if ( is_cart() ) {
            add_action( 'template_redirect', 'acau_enqueue_script' );
        }
    } );
    
  • Hook to template_redirect, then hook acau_enqueue_script to wp_footer (WooCommerce uses this hook to print custom JavaScript code added via wc_enqueue_js()):

    add_action( 'template_redirect', function(){
        if ( is_cart() ) {
            add_action( 'wp_footer', 'acau_enqueue_script' );
        }
    } );
    

Leave a Comment

tech