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/taxonomy.php

    r41987 r43510  
    12791279
    12801280/**
     1281 * Registers a meta key for terms.
     1282 *
     1283 * @since 4.9.8
     1284 *
     1285 * @param string $taxonomy Taxonomy to register a meta key for. Pass an empty string
     1286 *                         to register the meta key across all existing taxonomies.
     1287 * @param string $meta_key The meta key to register.
     1288 * @param array  $args     Data used to describe the meta key when registered. See
     1289 *                         {@see register_meta()} for a list of supported arguments.
     1290 * @return bool True if the meta key was successfully registered, false if not.
     1291 */
     1292function register_term_meta( $taxonomy, $meta_key, array $args ) {
     1293    $args['object_subtype'] = $taxonomy;
     1294
     1295    return register_meta( 'term', $meta_key, $args );
     1296}
     1297
     1298/**
     1299 * Unregisters a meta key for terms.
     1300 *
     1301 * @since 4.9.8
     1302 *
     1303 * @param string $taxonomy Taxonomy the meta key is currently registered for. Pass
     1304 *                         an empty string if the meta key is registered across all
     1305 *                         existing taxonomies.
     1306 * @param string $meta_key The meta key to unregister.
     1307 * @return bool True on success, false if the meta key was not previously registered.
     1308 */
     1309function unregister_term_meta( $taxonomy, $meta_key ) {
     1310    return unregister_meta_key( 'term', $meta_key, $taxonomy );
     1311}
     1312
     1313/**
    12811314 * Check if Term exists.
    12821315 *
Note: See TracChangeset for help on using the changeset viewer.