函数文档

remove_allowed_options()

💡 云策文档标注

概述

remove_allowed_options() 函数用于从 WordPress 的允许选项列表中移除指定选项。它接受一个要删除的选项数组作为参数,并返回更新后的允许选项数组。

关键要点

  • 函数参数:$del_options(必需,数组类型,指定要删除的选项,结构为页面键对应选项键数组)和 $options(可选,字符串或数组,默认为空时使用全局 $allowed_options)
  • 函数行为:遍历 $del_options,从 $allowed_options 中移除匹配的选项键,并返回修改后的数组
  • 相关函数:remove_option_whitelist() 是其已弃用的别名,功能相同
  • 版本历史:自 WordPress 5.5.0 版本引入

代码示例

function remove_allowed_options( $del_options, $options = '' ) {
    if ( '' === $options ) {
        global $allowed_options;
    } else {
        $allowed_options = $options;
    }

    foreach ( $del_options as $page => $keys ) {
        foreach ( $keys as $key ) {
            if ( isset( $allowed_options[ $page ] ) && is_array( $allowed_options[ $page ] ) ) {
                $pos = array_search( $key, $allowed_options[ $page ], true );
                if ( false !== $pos ) {
                    unset( $allowed_options[ $page ][ $pos ] );
                }
            }
        }
    }

    return $allowed_options;
}

注意事项

  • 使用此函数前需了解 $allowed_options 的结构,通常用于管理后台设置页面
  • 如果 $options 参数不为空,函数将使用传入的数组而非全局变量,这允许在特定上下文中操作
  • 移除选项可能影响相关功能,建议在插件或主题开发中谨慎使用

📄 原文内容

Removes a list of options from the allowed options list.

Parameters

$del_optionsarrayrequired
$optionsstring|arrayrequired

Return

array

Source

function remove_allowed_options( $del_options, $options = '' ) {
	if ( '' === $options ) {
		global $allowed_options;
	} else {
		$allowed_options = $options;
	}

	foreach ( $del_options as $page => $keys ) {
		foreach ( $keys as $key ) {
			if ( isset( $allowed_options[ $page ] ) && is_array( $allowed_options[ $page ] ) ) {
				$pos = array_search( $key, $allowed_options[ $page ], true );
				if ( false !== $pos ) {
					unset( $allowed_options[ $page ][ $pos ] );
				}
			}
		}
	}

	return $allowed_options;
}

Changelog

Version Description
5.5.0 Introduced.