函数文档

remove_theme_mod()

💡 云策文档标注

概述

remove_theme_mod() 函数用于从活动主题列表中移除指定的主题修改名称。如果移除后所有元素都被删除,整个选项将被移除。

关键要点

  • 函数接受一个必需的字符串参数 $name,指定要移除的主题修改名称。
  • 内部逻辑:首先获取所有主题修改,检查是否存在指定名称,然后移除它;如果移除后修改列表为空,则调用 remove_theme_mods() 删除整个选项。
  • 更新选项时使用 update_option() 保存修改后的列表到数据库,键名为 "theme_mods_$theme",其中 $theme 是活动主题的样式表名称。

代码示例

function remove_theme_mod( $name ) {
    $mods = get_theme_mods();

    if ( ! isset( $mods[ $name ] ) ) {
        return;
    }

    unset( $mods[ $name ] );

    if ( empty( $mods ) ) {
        remove_theme_mods();
        return;
    }

    $theme = get_option( 'stylesheet' );

    update_option( "theme_mods_$theme", $mods );
}

注意事项

  • 函数在 WordPress 2.1.0 版本中引入。
  • 相关函数包括 remove_theme_mods()、get_theme_mods()、update_option() 和 get_option()。
  • 该函数被多个核心功能使用,如 Custom_Image_Header::set_header_image() 和 switch_theme()。

📄 原文内容

Removes theme modification name from active theme list.

Description

If removing the name also removes all elements, then the entire option will be removed.

Parameters

$namestringrequired
Theme modification name.

Source

function remove_theme_mod( $name ) {
	$mods = get_theme_mods();

	if ( ! isset( $mods[ $name ] ) ) {
		return;
	}

	unset( $mods[ $name ] );

	if ( empty( $mods ) ) {
		remove_theme_mods();
		return;
	}

	$theme = get_option( 'stylesheet' );

	update_option( "theme_mods_$theme", $mods );
}

Changelog

Version Description
2.1.0 Introduced.