wp_deregister_style()


WordPress関数wp_deregister_style()は、特定のスタイルシートを登録解除するために使用されます。テーマやプラグインで不要なスタイルシートの読み込みを防ぐために役立ちます。

構文

wp_deregister_style( string $handle );
  • $handle (string) — 登録解除するスタイルのハンドル名。

例1: 不要なプラグインスタイルの登録解除

プラグインが読み込むスタイルを削除する基本的な例です。

add_action( 'wp_enqueue_scripts', function() {
    wp_deregister_style( 'plugin-style-handle' );
}, 20 );

例2: 条件付きでスタイルを登録解除

特定のページでのみスタイルを登録解除します。

add_action( 'wp_enqueue_scripts', function() {
    if ( is_page( 'contact' ) ) {
        wp_deregister_style( 'plugin-style-handle' );
    }
}, 20 );

例3: 管理画面でスタイルを登録解除

管理画面で読み込まれるスタイルを削除します。

add_action( 'admin_enqueue_scripts', function() {
    wp_deregister_style( 'admin-style-handle' );
} );

例4: 別のスタイルで置き換える

登録解除したスタイルを独自のスタイルで置き換えます。

add_action( 'wp_enqueue_scripts', function() {
    wp_deregister_style( 'plugin-style-handle' );
    wp_enqueue_style( 'custom-style', get_template_directory_uri() . '/css/custom.css' );
}, 20 );

例5: 特定のデバイスでスタイルを登録解除

モバイルデバイスでのみスタイルを登録解除します。

add_action( 'wp_enqueue_scripts', function() {
    if ( wp_is_mobile() ) {
        wp_deregister_style( 'plugin-style-handle' );
    }
}, 20 );

注意事項

  • 登録解除されたスタイルは再登録しない限り再度使用できません。
  • 他のテーマやプラグインで必要なスタイルを登録解除しないよう注意してください。

関連機能: