由于对其他functons.php代码段中的wp_enque_scripts执行操作,排队脚本失败


Dequeuing script fails because of action on wp_enqueue_scripts in other functons.php snippet

我想删除密码强度计(我读到它是基于wooccommerce,而不是基于wordpress)。我有一个片段,它的工作原理很有魅力,但有一个例外——它与我的第二个片段冲突,这阻止了这个脚本的排队。下面的第一个片段:

// Remove password strength meter from checkout, registration and password recovery.
function wc_ninja_remove_password_strength() {
    if ( wp_script_is( 'wc-password-strength-meter', 'enqueued' ) ) {
        wp_dequeue_script( 'wc-password-strength-meter' );      
    }
}
add_action( 'wp_print_scripts', 'wc_ninja_remove_password_strength' );

在我的第二个代码片段的函数中,我使用wp_print_scripts和wp_print_head_scripts进行了额外的添加和删除操作,但通过测试,我将导致问题的操作分离出来,只显示以下操作:

// Move render-blocking JavaScript.
function custom_clean_head() {    
   remove_action('wp_head', 'wp_enqueue_scripts', 1);  
   add_action('wp_footer', 'wp_enqueue_scripts', 5);
}
add_action( 'wp_enqueue_scripts', 'custom_clean_head', 9999999 );

我尝试注销脚本,密码强度计消失了,但它在页面加载上造成了大量的javascript错误。

我找到了一个解决方案,必须在第一个代码段的最后一行用wp_enqueue_scripts替换wp_print_scripts

事实证明,由于Wordpress 3.3(4年多前),wp_print_scripts不应该像我刚才那样被wp_enqueue_scripts取代。

https://codex.wordpress.org/Plugin_API/Action_Reference/wp_print_scripts

// Remove password strength meter from checkout, registration and password recovery.
function wc_ninja_remove_password_strength() {
    if ( wp_script_is( 'wc-password-strength-meter', 'enqueued' ) ) {
        wp_dequeue_script( 'wc-password-strength-meter' );      
    }
}
add_action( 'wp_enqueue_scripts', 'wc_ninja_remove_password_strength');