如何将任何主题样式和脚本出队/注销

时间:2019-06-18 作者:user315338

有没有可能让我的插件退出或取消注册任何主题激活的样式和脚本。因此,无论安装哪个主题,该主题的样式和脚本都将被出列/注销?

非常清楚的是:

我不知道会用什么主题

4 个回复
最合适的回答,由SO网友:Jacob Peattie 整理而成

棘手的是要知道某个特定的脚本或样式是否由主题排队。

主题和插件都使用相同的挂钩和函数,因此它们没有以任何方式明确标记为属于特定主题或插件。这意味着知道脚本或样式是否来自主题的唯一方法是检查URL,以查看脚本/样式的URL是否指向主题目录中的某个位置。

一种方法是循环$wp_scripts->registered$wp_styles->registered, 并检查每个脚本和样式的URLget_theme_root_uri() 它告诉您主题文件夹的URL。如果脚本/样式似乎位于该文件夹内,则可以将其出列:

function wpse_340767_dequeue_theme_assets() {
    $wp_scripts = wp_scripts();
    $wp_styles  = wp_styles();
    $themes_uri = get_theme_root_uri();

    foreach ( $wp_scripts->registered as $wp_script ) {
        if ( strpos( $wp_script->src, $themes_uri ) !== false ) {
            wp_deregister_script( $wp_script->handle );
        }
    }

    foreach ( $wp_styles->registered as $wp_style ) {
        if ( strpos( $wp_style->src, $themes_uri ) !== false ) {
            wp_deregister_style( $wp_style->handle );
        }
    }
}
add_action( \'wp_enqueue_scripts\', \'wpse_340767_dequeue_theme_assets\', 999 );
只有当样式表或脚本位于主题内时,这才有效。如果主题是将来自CDN的脚本或样式排入队列,那么我不确定是否可以将其作为目标。

SO网友:Chetan Vaghela

也许这对你有帮助。尝试

#For dequeue JavaScripts
function remove_unnecessary_scripts() {
    # pass Name of the enqueued js.
    # dequeue js
    wp_dequeue_script( \'toaster-js\' );
     # deregister js
    wp_deregister_script( \'toaster-js\' );
}
add_action( \'wp_print_scripts\', \'remove_unnecessary_scripts\' );

#For dequeue Styles 
function remove_unnecessary_styles() {
    # pass Name of the enqueued stylesheet.
    # dequeue style
    wp_dequeue_style( \'custom-style\' );
    # deregister style
    wp_deregister_style( \'custom-style\' );
}
add_action( \'wp_print_styles\', \'remove_unnecessary_styles\' );
对于仅删除主题样式和脚本,您可以尝试以下操作:

function remove_all_scripts_from_theme() {
    global $wp_scripts;
    # remove all js
    // $wp_scripts->queue = array();
    foreach( $wp_scripts->queue as $handle ) {

        if (strpos($wp_scripts->registered[$handle]->src, \'/themes/\') !== false) {
            # dequeue js
              wp_dequeue_script( $handle );
                # deregister js
               wp_deregister_script( $handle);
            }
        }

}
add_action(\'wp_print_scripts\', \'remove_all_scripts_from_theme\', 100);

function remove_all_styles_from_theme() {
    global $wp_styles;
     # remove all css
   // $wp_styles->queue = array();

    foreach( $wp_styles->queue as $handle ) {

        if (strpos($wp_styles->registered[$handle]->src, \'/themes/\') !== false) {
            # dequeue js
              wp_dequeue_style( $handle );
                # deregister js
               wp_deregister_style( $handle);
            }
        }

}
add_action(\'wp_print_styles\', \'remove_all_styles_from_theme\', 100);
让我知道它是否有效。我已经测试了这个代码。它就像魔咒一样工作:-)

非常感谢。

SO网友:hayatbiralem

如果没有主题,主题的功能。php永远不会运行,因此应按预期工作:

function myprefix_disable_theme_load() {
    return "";
}
add_filter("template_directory", "myprefix_disable_theme_load", 1, 0);
add_filter("stylesheet_directory", "myprefix_disable_theme_load", 1, 0);
但请注意,这会从主题中删除我们在本例中想要的所有功能。

SO网友:Bhupen

您可以使用wp\\u print\\u脚本删除插件或父主题中排队的脚本和样式。请参见演示代码:

/**
 * Dequeue the Parent Theme and Plugins scripts and styles
 */
function custom_dequeue_script() {
    wp_dequeue_script( \'script-handle\' );
    wp_dequeue_style(\'script-handle\');
    wp_deregister_style(\'script-handle\');
}

add_action( \'wp_print_scripts\', \'custom_dequeue_script\', 100 );
请尝试此新代码从激活的主题中删除脚本和样式:

function remove_theme_all_scripts() {
global $wp_scripts;

$stylesheet_uri = get_stylesheet_directory_uri();
$new_scripts_list = array(); 

foreach( $wp_scripts->queue as $handle ) {
    $obj = $wp_scripts->registered[$handle];
    $obj_handle = $obj->handle;
    $obj_uri = $obj->src;

    if ( strpos( $obj_uri, $stylesheet_uri ) === 0 )  {
        //Do Nothing
    } else {
        $new_scripts_list[] = $obj_handle;
    }
}

$wp_scripts->queue = $new_scripts_list;
}
add_action(\'wp_print_scripts\', \'remove_theme_all_scripts\', 100);

function remove_theme_all_styles() {
global $wp_styles;
$stylesheet_uri = get_stylesheet_directory_uri();
$new_styles_list = array(); 

foreach( $wp_styles->queue as $handle ) {
    $obj = $wp_styles->registered[$handle];
    $obj_handle = $obj->handle;
    $obj_uri = $obj->src;

    if ( strpos( $obj_uri, $stylesheet_uri ) === 0 )  {
        //Do Nothing
    } else {
        $new_styles_list[] = $obj_handle;
    }
}

$wp_styles->queue = $new_styles_list;
}
add_action(\'wp_print_styles\', \'remove_theme_all_styles\', 100);

相关推荐