避免在登录屏幕中加载默认的WP样式

时间:2013-09-09 作者:AlbertoFdzM

有没有办法避免在登录屏幕中加载默认的WordPress样式,而只加载我的自定义样式表?

实际上,我使用的是以下代码:

function login_styles() {
    // Sets the styles if is the current theme
    if (get_stylesheet_directory() == get_template_directory()) {
        // Prints link to stylesheed login.css
        wp_register_style(...);
        wp_enqueue_style(...);
    }
}
add_action( \'login_enqueue_scripts\', \'login_styles\' );
它工作得很好,但WordPress首先加载它的样式。我想从此屏幕中排除该文件。

3 个回复
最合适的回答,由SO网友:Spencer 整理而成

使用wp_deregister_style( \'login\' ) 删除登录样式仍将导致404\'d请求登录CSS文件。但是,如果在取消注册后重新注册登录样式,则可以防止不必要的请求。

add_action( \'login_init\', function() {
    wp_deregister_style( \'login\' );
    wp_register_style( \'login\' );
} );
这将给您留下一个完全没有样式的wp-login.php 页要重新设置样式,您可以在login 手柄

这可能比搞乱style_loader_tag 滤器

<小时>

Update:

login_enqueue_scripts 是一个更合适的操作,退出队列不会导致404。

add_action( \'login_enqueue_scripts\', function() {
    wp_dequeue_style( \'login\' );
} );

SO网友:brasofilo

在删除的回答中,他指出登录样式打印为wp_admin_css() *,我找到了一个过滤器,style_loader_tag. 但进一步的测试表明wp_deregister_style 足够了。

反馈和进一步的测试表明,取消注册该样式会为wp-admin.css, 所以回到loader标签:

add_action( \'login_init\', function() 
{
    # Produces a 404 for the style
    // wp_deregister_style( \'wp-admin\' );

    # The following is a filter in /wp-includes/class.wp-styles.php
    # Completely cleans up the <head> of enqueues
    add_filter( \'style_loader_tag\', \'__return_null\' );
});
<子>* 这个函数中甚至有一个过滤器,但它似乎无法捕获已注册的样式

SO网友:AlbertoFdzM

找到了从登录屏幕中删除样式的方法:

function login_styles_reset( $style ) {
    if ( strpos( $style, \'wp-admin-css\') !== FALSE ) {
        $style = NULL;
    } elseif ( strpos( $style, \'colors-fresh-css\') !== FALSE ) {
        $style = NULL;
    }
    return $style;
}

function custom_login_styles() {
    add_filter( \'style_loader_tag\', \'login_styles_reset\' );

    // Code for the new login styles
}
add_action( \'login_init\', \'custom_login_styles\');
希望这能帮助别人。

结束

相关推荐

这种CSS填充是有效的,但这是一种好做法吗?

我正在尝试在我的插件中包含动态css,我一直在搜索任何地方,但没有找到任何与我所做类似的情况。我使用的方法是,在关闭php标记后,将css文件直接添加到插件php文件的末尾?> 无需添加任何其他内容,如呼叫wp head等。下面是我直接放入插件文件末尾的代码示例:<style type=\"text/css\"> .innertrow { background-color: <?php get_options(\'css_value\');?>}&#x