Make WordPress Core


Ignore:
Timestamp:
10/08/2015 07:06:32 PM (10 years ago)
Author:
DrewAPicture
Message:

Administration: Add the ability to pass an array of screen IDs to add_meta_box() and remove_meta_box().

The $screen parameter in both functions can now accept a single screen ID, WP_Screen object, or an array of screen IDs.

Adds tests.

Props coffee2code, iamfriendly, madalinungureanu, mordauk, igmoweb, meloniq, DrewAPicture.
See #15000.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/template-functions.php

    r34893 r34951  
    848848
    849849/**
    850  * Add a meta box to an edit form.
     850 * Adds a meta box to one or more screens.
    851851 *
    852852 * @since 2.5.0
     853 * @since 4.4.0 The `$screen` parameter now accepts an array of screen IDs.
    853854 *
    854855 * @global array $wp_meta_boxes
     
    858859 * @param callable         $callback      Function that fills the box with the desired content.
    859860 *                                        The function should echo its output.
    860  * @param string|WP_Screen $screen        Optional. The screen on which to show the box (like a post
    861  *                                        type, 'link', or 'comment'). Default is the current screen.
     861 * @param string|array|WP_Screen $screen  Optional. The screen or screens on which to show the box
     862 *                                        (such as a post type, 'link', or 'comment'). Accepts a single
     863 *                                        screen ID, WP_Screen object, or array of screen IDs. Default
     864 *                                        is the current screen.
    862865 * @param string           $context       Optional. The context within the screen where the boxes
    863866 *                                        should display. Available contexts vary from screen to
     
    875878    global $wp_meta_boxes;
    876879
    877     if ( empty( $screen ) )
     880    if ( empty( $screen ) ) {
    878881        $screen = get_current_screen();
    879     elseif ( is_string( $screen ) )
     882    } elseif ( is_string( $screen ) ) {
    880883        $screen = convert_to_screen( $screen );
     884    } elseif ( is_array( $screen ) ) {
     885        foreach ( $screen as $single_screen ) {
     886            add_meta_box( $id, $title, $callback, $single_screen, $context, $priority, $callback_args );
     887        }
     888    }
     889
     890    if ( ! isset( $screen->id ) ) {
     891        return;
     892    }
    881893
    882894    $page = $screen->id;
     
    10121024
    10131025/**
    1014  * Remove a meta box from an edit form.
     1026 * Removes a meta box from one or more screens.
    10151027 *
    10161028 * @since 2.6.0
     1029 * @since 4.4.0 The `$screen` parameter now accepts an array of screen IDs.
    10171030 *
    10181031 * @global array $wp_meta_boxes
    10191032 *
    10201033 * @param string        $id      String for use in the 'id' attribute of tags.
    1021  * @param string|object $screen  The screen on which to show the box (post, page, link).
     1034 * @param string|array|WP_Screen $screen The screen or screens on which the meta box is shown (such as a
     1035 *                                       post type, 'link', or 'comment'). Accepts a single screen ID,
     1036 *                                       WP_Screen object, or array of screen IDs.
    10221037 * @param string        $context The context within the page where the boxes should show ('normal', 'advanced').
    10231038 */
    1024 function remove_meta_box($id, $screen, $context) {
     1039function remove_meta_box( $id, $screen, $context ) {
    10251040    global $wp_meta_boxes;
    10261041
    1027     if ( empty( $screen ) )
     1042    if ( empty( $screen ) ) {
    10281043        $screen = get_current_screen();
    1029     elseif ( is_string( $screen ) )
     1044    } elseif ( is_string( $screen ) ) {
    10301045        $screen = convert_to_screen( $screen );
     1046    } elseif ( is_array( $screen ) ) {
     1047        foreach ( $screen as $single_screen ) {
     1048            remove_meta_box( $id, $single_screen, $context );
     1049        }
     1050    }
     1051
     1052    if ( ! isset( $screen->id ) ) {
     1053        return;
     1054    }
    10311055
    10321056    $page = $screen->id;
Note: See TracChangeset for help on using the changeset viewer.