WordPress函数register_meta()注册一个自定义附加信息的metabox键

描述:

注册一个自定义附加信息的metabox键

用法:

<?php register_meta($meta_type, $meta_key, $sanitize_callback, $auth_callback, $args );  ?> 

参数:

$meta_type

(string) (必填) meta类型

默认值: None

$meta_key

(string) (必填) meta键

默认值: None

$sanitize_callback

(mixed) (必填) 字符串或数组。

默认值: None

$auth_callback

(mixed) (可选) 字符串或数组。执行edit_post_meta、add_post_meta和delete_post_meta功能检查时要调用的函数或方法。

默认值: null

示例:

<?php register_meta( 'post', 'my_registered_meta', 'my_sanitize_callback', '__return_false' ); ?>

源文件:

function register_meta( $object_type, $meta_key, $args, $deprecated = null ) {
    global $wp_meta_keys;
 
    if ( ! is_array( $wp_meta_keys ) ) {
        $wp_meta_keys = array();
    }
 
    $defaults = array(
        'object_subtype'    => '',
        'type'              => 'string',
        'description'       => '',
        'single'            => false,
        'sanitize_callback' => null,
        'auth_callback'     => null,
        'show_in_rest'      => false,
    );
 
    // There used to be individual args for sanitize and auth callbacks
    $has_old_sanitize_cb = false;
    $has_old_auth_cb     = false;
 
    if ( is_callable( $args ) ) {
        $args = array(
            'sanitize_callback' => $args,
        );
 
        $has_old_sanitize_cb = true;
    } else {
        $args = (array) $args;
    }
 
    if ( is_callable( $deprecated ) ) {
        $args['auth_callback'] = $deprecated;
        $has_old_auth_cb       = true;
    }
 
    /**
     * Filters the registration arguments when registering meta.
     *
     * @since 4.6.0
     *
     * @param array  $args        Array of meta registration arguments.
     * @param array  $defaults    Array of default arguments.
     * @param string $object_type Object type.
     * @param string $meta_key    Meta key.
     */
    $args = apply_filters( 'register_meta_args', $args, $defaults, $object_type, $meta_key );
    $args = wp_parse_args( $args, $defaults );
 
    // Require an item schema when registering array meta.
    if ( false !== $args['show_in_rest'] && 'array' === $args['type'] ) {
        if ( ! is_array( $args['show_in_rest'] ) || ! isset( $args['show_in_rest']['schema']['items'] ) ) {
            _doing_it_wrong( __FUNCTION__, __( 'When registering an "array" meta type to show in the REST API, you must specify the schema for each array item in "show_in_rest.schema.items".' ), '5.3.0' );
 
            return false;
        }
    }
 
    $object_subtype = ! empty( $args['object_subtype'] ) ? $args['object_subtype'] : '';
 
    // If `auth_callback` is not provided, fall back to `is_protected_meta()`.
    if ( empty( $args['auth_callback'] ) ) {
        if ( is_protected_meta( $meta_key, $object_type ) ) {
            $args['auth_callback'] = '__return_false';
        } else {
            $args['auth_callback'] = '__return_true';
        }
    }
 
    // Back-compat: old sanitize and auth callbacks are applied to all of an object type.
    if ( is_callable( $args['sanitize_callback'] ) ) {
        if ( ! empty( $object_subtype ) ) {
            add_filter( "sanitize_{$object_type}_meta_{$meta_key}_for_{$object_subtype}", $args['sanitize_callback'], 10, 4 );
        } else {
            add_filter( "sanitize_{$object_type}_meta_{$meta_key}", $args['sanitize_callback'], 10, 3 );
        }
    }
 
    if ( is_callable( $args['auth_callback'] ) ) {
        if ( ! empty( $object_subtype ) ) {
            add_filter( "auth_{$object_type}_meta_{$meta_key}_for_{$object_subtype}", $args['auth_callback'], 10, 6 );
        } else {
            add_filter( "auth_{$object_type}_meta_{$meta_key}", $args['auth_callback'], 10, 6 );
        }
    }
 
    // Global registry only contains meta keys registered with the array of arguments added in 4.6.0.
    if ( ! $has_old_auth_cb && ! $has_old_sanitize_cb ) {
        unset( $args['object_subtype'] );
 
        $wp_meta_keys[ $object_type ][ $object_subtype ][ $meta_key ] = $args;
 
        return true;
    }
 
    return false;
}