admin管理员组文章数量:1416080
In my plugin, I'm trying to add inline script using wc_enqueue_js
only on the cart page. The rest of the plugin is also executed only on the cart page.
The code below works fine, but I don't like the fact that I need to repeat if (! is_cart() ) return;
. This may not look like a big issue, but my other plugin with a similar problem has several lines of conditions based on what the user selected in plugin settings, that are mixed with multiple actions:
// Run plugin.
add_action( 'template_redirect', function() use ( $args ) { ajax_cart_autoupdate( $args ); });
function ajax_cart_autoupdate( $args ) {
if (! is_cart() ) return; // Only if it's a cart page.
// Do stuff.
}
// Enqueue js script inline using wc_enqueue_js.
add_action( 'template_redirect', function() use ( $args ) { acau_enqueue_script ( $args ); });
function acau_enqueue_script( $args ) {
if (! is_cart() ) return; // Only if it's a cart page.
wc_enqueue_js( '
<<<my jquery code>>>
' );
}
The following doesn't work - the inline script doesn't appear in the HTML:
// Run plugin.
add_action( 'template_redirect', function() use ( $args ) { ajax_cart_autoupdate( $args ); });
function ajax_cart_autoupdate( $args ) {
if (! is_cart() ) return; // Only if it's a cart page.
// Enqueue js script inline using wc_enqueue_js.
add_action( 'template_redirect', function() use ( $args ) { acau_enqueue_script ( $args ); });
// Do stuff.
}
function acau_enqueue_script( $args ) {
wc_enqueue_js( '
<<<my jquery code>>>
' );
}
The following works, but the inline script appears in the HTML twice (one script directly below the other). If I put echo 'test';
in the first line of function acau_enqueue_script
, it appears only one time on page:
// Run plugin.
add_action( 'template_redirect', function() use ( $args ) { ajax_cart_autoupdate( $args ); });
function ajax_cart_autoupdate( $args ) {
if (! is_cart() ) return; // Only if it's a cart page.
// Enqueue js script inline using wc_enqueue_js.
add_action( 'wp_enqueue_script', function() use ( $args ) { acau_enqueue_script ( $args ); });
// Do stuff.
}
function acau_enqueue_script( $args ) {
wc_enqueue_js( '
<<<my jquery code>>>
' );
}
Is there any smart way to make it work without repeating page conditions?
In my plugin, I'm trying to add inline script using wc_enqueue_js
only on the cart page. The rest of the plugin is also executed only on the cart page.
The code below works fine, but I don't like the fact that I need to repeat if (! is_cart() ) return;
. This may not look like a big issue, but my other plugin with a similar problem has several lines of conditions based on what the user selected in plugin settings, that are mixed with multiple actions:
// Run plugin.
add_action( 'template_redirect', function() use ( $args ) { ajax_cart_autoupdate( $args ); });
function ajax_cart_autoupdate( $args ) {
if (! is_cart() ) return; // Only if it's a cart page.
// Do stuff.
}
// Enqueue js script inline using wc_enqueue_js.
add_action( 'template_redirect', function() use ( $args ) { acau_enqueue_script ( $args ); });
function acau_enqueue_script( $args ) {
if (! is_cart() ) return; // Only if it's a cart page.
wc_enqueue_js( '
<<<my jquery code>>>
' );
}
The following doesn't work - the inline script doesn't appear in the HTML:
// Run plugin.
add_action( 'template_redirect', function() use ( $args ) { ajax_cart_autoupdate( $args ); });
function ajax_cart_autoupdate( $args ) {
if (! is_cart() ) return; // Only if it's a cart page.
// Enqueue js script inline using wc_enqueue_js.
add_action( 'template_redirect', function() use ( $args ) { acau_enqueue_script ( $args ); });
// Do stuff.
}
function acau_enqueue_script( $args ) {
wc_enqueue_js( '
<<<my jquery code>>>
' );
}
The following works, but the inline script appears in the HTML twice (one script directly below the other). If I put echo 'test';
in the first line of function acau_enqueue_script
, it appears only one time on page:
// Run plugin.
add_action( 'template_redirect', function() use ( $args ) { ajax_cart_autoupdate( $args ); });
function ajax_cart_autoupdate( $args ) {
if (! is_cart() ) return; // Only if it's a cart page.
// Enqueue js script inline using wc_enqueue_js.
add_action( 'wp_enqueue_script', function() use ( $args ) { acau_enqueue_script ( $args ); });
// Do stuff.
}
function acau_enqueue_script( $args ) {
wc_enqueue_js( '
<<<my jquery code>>>
' );
}
Is there any smart way to make it work without repeating page conditions?
Share Improve this question edited Aug 14, 2019 at 4:41 Ryszard Jędraszyk asked Aug 14, 2019 at 0:55 Ryszard JędraszykRyszard Jędraszyk 3244 silver badges17 bronze badges 1 |1 Answer
Reset to default 2No 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 childadd_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 is12
, 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()
andsome_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 hookacau_enqueue_script()
totemplate_redirect
:add_action( 'wp', function(){ if ( is_cart() ) { add_action( 'template_redirect', 'acau_enqueue_script' ); } } );
Hook to
template_redirect
, then hookacau_enqueue_script
towp_footer
(WooCommerce uses this hook to print custom JavaScript code added viawc_enqueue_js()
):add_action( 'template_redirect', function(){ if ( is_cart() ) { add_action( 'wp_footer', 'acau_enqueue_script' ); } } );
本文标签: phpUse wcenqueuejs only on specific pagesnested addaction
版权声明:本文标题:php - Use wc_enqueue_js only on specific pages - nested add_action 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745244540a2649496.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
add_action
from the default (10) to 20, causing the parent and childadd_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);
I would be grateful for an answer why this is the case. – Ryszard Jędraszyk Commented Aug 14, 2019 at 1:43