Make WordPress Core


Ignore:
Timestamp:
07/19/2018 06:48:52 PM (7 years ago)
Author:
kadamwhite
Message:

REST API: Support meta registration for specific object subtypes.

Introduce an object_subtype argument to the args array for register_meta() which can be used to limit meta registration to a single subtype (e.g. a custom post type or taxonomy, vs all posts or taxonomies).

Introduce register_post_meta() and register_term_meta() wrapper methods for register_meta to provide a convenient interface for the common case of registering meta for a specific taxonomy or post type. These methods work the way plugin developers have often expected register_meta to function, and should be used in place of direct register_meta where possible.

Props flixos90, tharsheblows, spacedmonkey.

Merges [43378] to the 4.9 branch.
Fixes #38323.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/4.9/src/wp-includes/post.php

    r43393 r43510  
    18851885
    18861886/**
     1887 * Registers a meta key for posts.
     1888 *
     1889 * @since 4.9.8
     1890 *
     1891 * @param string $post_type Post type to register a meta key for. Pass an empty string
     1892 *                          to register the meta key across all existing post types.
     1893 * @param string $meta_key  The meta key to register.
     1894 * @param array  $args      Data used to describe the meta key when registered. See
     1895 *                          {@see register_meta()} for a list of supported arguments.
     1896 * @return bool True if the meta key was successfully registered, false if not.
     1897 */
     1898function register_post_meta( $post_type, $meta_key, array $args ) {
     1899    $args['object_subtype'] = $post_type;
     1900
     1901    return register_meta( 'post', $meta_key, $args );
     1902}
     1903
     1904/**
     1905 * Unregisters a meta key for posts.
     1906 *
     1907 * @since 4.9.8
     1908 *
     1909 * @param string $post_type Post type the meta key is currently registered for. Pass
     1910 *                          an empty string if the meta key is registered across all
     1911 *                          existing post types.
     1912 * @param string $meta_key  The meta key to unregister.
     1913 * @return bool True on success, false if the meta key was not previously registered.
     1914 */
     1915function unregister_post_meta( $post_type, $meta_key ) {
     1916    return unregister_meta_key( 'post', $meta_key, $post_type );
     1917}
     1918
     1919/**
    18871920 * Retrieve post meta fields, based on post ID.
    18881921 *
Note: See TracChangeset for help on using the changeset viewer.