sanitize_meta() | 清理WordPress元数据中的非法字符

猫斯基 9 2015-01-25

函数

sanitize_meta( string $meta_key, mixed $meta_value, string $object_type, string $object_subtype = '' )

描述

清理WordPress元数据中的非法字符

参数

$meta_key,( string ) (必需) 元数据键。

$meta_value,(混合) (必需) 要清理的元数据值。

$object_type,( string ) (必需) 对象元数据的类型。接受“post”、“comment”、“term”、“user”或任何其他具有关联元表的对象类型。

$object_subtype,( string ) (可选) 对象类型的子类型。默认值: ''

返回值

(mixed)清理后的 $meta_value

建站迷·划重点

相关函数

来源

文件:wp-includes/meta.php

更新日志

版本 描述
4.9.8 $object_subtype加入参数。
3.1.3 介绍。

示例

$clean_value = sanitize_meta( 'birth-year', $user_input, 'user' );
 
function wpdocs_sanitize_birth_year_meta( $year ) {
 
    $now  = date( 'Y' );
    $then = $now - 115; // No users older than 115.
 
    if ( $then > $year || $year > $now ) {
        wp_die( __( 'Invalid entry, go back and try again.', 'textdomain' ) );
    }
    return $year;
}
add_filter( 'sanitize_user_meta_birth-year', 'wpdocs_sanitize_birth_year_meta' );

本文由 猫斯基 原创发布。

著作权均归用户本人所有。独家文章转载,请联系本站管理员。获得授权后,须注明本文地址! 本文地址:https://www.maosiji.com/wordpress-sanitize_meta.html