函数文档

wp_deregister_style()

💡 云策文档标注

概述

wp_deregister_style() 函数用于移除已注册的样式表,基于 WP_Dependencies::remove() 实现。开发者可通过指定样式表句柄来调用此函数,常用于优化前端资源加载。

关键要点

  • 函数作用:移除已注册的样式表,防止其在前端加载。
  • 参数:$handle(字符串,必需),指定要移除的样式表名称。
  • 内部实现:调用 wp_styles()->remove($handle) 来执行移除操作。
  • 相关函数:wp_styles() 用于初始化 $wp_styles 对象。
  • 版本历史:自 WordPress 2.1.0 版本引入。

代码示例

add_action( 'wp_enqueue_scripts', 'remove_default_stylesheet', 20 );
function remove_default_stylesheet() {
    wp_dequeue_style( 'original-enqueue-stylesheet-handle' );
    wp_deregister_style( 'original-register-stylesheet-handle' );

    wp_register_style( 'new-style', get_stylesheet_directory_uri() . '/new.css', false, '1.0.0' ); 
    wp_enqueue_style( 'new-style' );
}

📄 原文内容

Removes a registered stylesheet.

Description

See also

Parameters

$handlestringrequired
Name of the stylesheet to be removed.

Source

function wp_deregister_style( $handle ) {
	_wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle );

	wp_styles()->remove( $handle );
}

Changelog

Version Description
2.1.0 Introduced.

User Contributed Notes

  1. Skip to note 2 content

    Example:

    add_action( 'wp_enqueue_scripts', 'remove_default_stylesheet', 20 );
    function remove_default_stylesheet() {
        wp_dequeue_style( 'original-enqueue-stylesheet-handle' );
        wp_deregister_style( 'original-register-stylesheet-handle' );
    
        wp_register_style( 'new-style', get_stylesheet_directory_uri() . '/new.css', false, '1.0.0' ); 
        wp_enqueue_style( 'new-style' );
    }