钩子文档

theme_mod_{$name}

💡 云策文档标注

概述

theme_mod_{$name} 是一个 WordPress 过滤器钩子,用于动态过滤主题修改(theme_mod)的值。它允许开发者在运行时修改特定主题选项的值,而不改变数据库中的存储值。

关键要点

  • 这是一个动态钩子,$name 部分对应主题修改数组的键名,如 'header_textcolor' 或 'header_image'。
  • 过滤器仅影响前端显示的值,不会修改数据库中存储的原始数据。
  • 常用于自定义器中调整主题选项的显示行为。

代码示例

// 过滤 customizer 中的 display_title_and_tagline 值
add_filter( 'theme_mod_display_title_and_tagline', 'wpdocs_display_title_and_tagline' );

function wpdocs_display_title_and_tagline( $display_title_and_tagline ) {
    return false;
}

注意事项

此过滤器在 WordPress 2.2.0 版本中引入,与 get_theme_mod() 函数相关,用于获取活动主题的修改值。


📄 原文内容

Filters the theme modification, or ‘theme_mod’, value.

Description

The dynamic portion of the hook name, $name, refers to the key name of the modification array. For example, ‘header_textcolor’, ‘header_image’, and so on depending on the theme options.

Parameters

$current_modmixed
The value of the active theme modification.

Source

return apply_filters( "theme_mod_{$name}", $mods[ $name ] );

Changelog

Version Description
2.2.0 Introduced.

User Contributed Notes

  1. Skip to note 2 content

    This filter will have an impact on the front-end, but not the value kept in the database.

    // filter to change the value of display_title_and_tagline in the customizer.
    add_filter( 'theme_mod_display_title_and_tagline', 'wpdocs_display_title_and_tagline' );
    
    function wpdocs_display_title_and_tagline( $display_title_and_tagline ) {
    	return false;
    }