add_site_meta()
云策文档标注
概述
add_site_meta() 函数用于向 WordPress 站点添加元数据,是 add_metadata() 的封装,专门处理站点级别的元数据操作。
关键要点
- 函数接受四个参数:$site_id(站点ID,整数,必需)、$meta_key(元数据键,字符串,必需)、$meta_value(元数据值,混合类型,必需)、$unique(是否唯一,布尔值,可选,默认false)。
- 元数据值支持多种类型:数组和对象会序列化存储,其他类型如布尔值、数字会转换为字符串存储。
- 返回值为元数据ID(整数)或失败时的false。
- 由于历史原因,输入时元数据键和值需要“转义斜杠”。
代码示例
function add_site_meta( $site_id, $meta_key, $meta_value, $unique = false ) {
return add_metadata( 'blog', $site_id, $meta_key, $meta_value, $unique );
}注意事项
- 该函数从 WordPress 5.1.0 版本开始引入。
- 相关函数包括 add_metadata(),用于更通用的元数据添加。
原文内容
Adds metadata to a site.
Description
For historical reasons both the meta key and the meta value are expected to be “slashed” (slashes escaped) on input.
Parameters
$site_idintrequired-
Site ID.
$meta_keystringrequired-
Metadata name.
$meta_valuemixedrequired-
Metadata value. Arrays and objects are stored as serialized data and will be returned as the same type when retrieved. Other data types will be stored as strings in the database:
- false is stored and retrieved as an empty string (
'') - true is stored and retrieved as
'1' - numbers (both integer and float) are stored and retrieved as strings Must be serializable if non-scalar.
- false is stored and retrieved as an empty string (
$uniquebooloptional-
Whether the same key should not be added.
Default:
false
Source
function add_site_meta( $site_id, $meta_key, $meta_value, $unique = false ) {
return add_metadata( 'blog', $site_id, $meta_key, $meta_value, $unique );
}
Changelog
| Version | Description |
|---|---|
| 5.1.0 | Introduced. |