如何禁用特定帖子类型页面的主题中的所有CSS

时间:2019-01-14 作者:J.BizMai

我正在创建一个插件,当管理员登录时single-{post-type-slug}.php 不存在,该页https://my-site/post-type-slug/my-admin-page.php 无需加载主题css即可显示。

我试过:

wp_dequeue_style(\'screen\');
wp_deregister_style(\'screen\');

wp_dequeue_style(\'style\');
wp_deregister_style(\'style\');


wp_dequeue_style(\'style.css\');
wp_deregister_style(\'style.css\');

wp_dequeue_style( \'wpsl-styles\' );
像这样在我的物体里面。。。

 add_action( \'wp_print_scripts\', array( $this , "remove_style" ), 100000 );

 or

 add_action( \'wp_enqueue_scripts\', array( $this , "remove_style" ), 100000 );


and

public function remove_style(){
    wp_dequeue_style( \'wpsl-styles\' );
    //wp_dequeue_style("style");
    //wp_deregister_style("style");
    global $wp_styles;
    foreach( $wp_styles->queue as $style ) :
        echo $wp_styles->registered[$style]->src . "<br/>";
    endforeach;
}
任何东西都不起作用(Wordpress 5.x),它是一个只能删除的系统style.css. 我想为我的插件找到一种方法来删除除WordPress管理栏之外的所有样式。

有人有主意吗?

1 个回复
最合适的回答,由SO网友:J.BizMai 整理而成

我找到了一种列出所有句柄的方法,然后,我选择了我想要保留的所有css来执行此功能:

//call in my object
add_action( \'wp_enqueue_scripts\', array( $this , "clean_styles" ), 9999 );

public function clean_styles(){
    wp_debug_log();
    global $wp_styles;
    foreach( $wp_styles->queue as $style ) :
        //List the css src and handle
        //echo $wp_styles->registered[$style]->src . " - ";
        //echo $wp_styles->registered[$style]->handle . "<br/>";
        $handle = $wp_styles->registered[$style]->handle;
        //array of css I want to keep
        $css_exception = [ "wp-block-library", "wp-block-library-theme", "admin-bar" ]
        if( !in_array( $handle, $css_exception ) ){
            wp_dequeue_style( $handle );
            wp_deregister_style( $handle );
        }
    endforeach;
        //echo "<br/><br/>";
    //List the css list I kept
    /*foreach( $wp_styles->queue as $style ) :
        echo $wp_styles->registered[$style]->src . "<br/>";
    endforeach;*/
}