WordPress.org

Make WordPress Core

Changeset 43882


Ignore:
Timestamp:
11/09/2018 09:05:47 AM (12 months ago)
Author:
pento
Message:

Meta Boxes: Add the block_editor_meta_box_hidden_fields action.

Lacking an appropriate action in the classic editor, plugins that add meta boxes have historically hooked into various actions in order to add hidden input fields.

This change also adds backwards compatibility for two of the most common: edit_form_after_title, and edit_form_advanced.

Props pento, danielbachhuber.
See #45283.

Location:
branches/5.0/src/wp-admin
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/5.0/src/wp-admin/edit-form-blocks.php

    r43878 r43882  
    387387register_and_do_post_meta_boxes( $post );
    388388
    389 // Some meta boxes hook into the 'edit_form_advanced' filter.
    390 /** This action is documented in wp-admin/edit-form-advanced.php */
    391 do_action( 'edit_form_advanced', $post );
    392 
    393389require_once( ABSPATH . 'wp-admin/admin-header.php' );
    394390?>
  • branches/5.0/src/wp-admin/includes/post.php

    r43880 r43882  
    21862186    $user_id      = $current_user->ID;
    21872187    wp_nonce_field( $nonce_action );
     2188
     2189    /*
     2190     * Some meta boxes hook into these actions to add hidden input fields in the classic post form. For backwards
     2191     * compatibility, we can capture the output from these actions, and extract the hidden input fields.
     2192     */
     2193    $actions = array(
     2194        'edit_form_after_title',
     2195        'edit_form_advanced',
     2196    );
     2197
     2198    foreach ( $actions as $action ) {
     2199        ob_start();
     2200        do_action_deprecated(
     2201            $action,
     2202            array( $post ),
     2203            '5.0.0',
     2204            'block_editor_meta_box_hidden_fields',
     2205            __( 'This action is still supported in the classic editor, but is deprecated in the block editor.' )
     2206        );
     2207        $classic_output = ob_get_clean();
     2208
     2209        if ( ! $classic_output ) {
     2210            continue;
     2211        }
     2212
     2213        $classic_elements = wp_html_split( $classic_output );
     2214        $hidden_inputs    = '';
     2215        foreach( $classic_elements as $element ) {
     2216            if ( 0 !== strpos( $element, '<input ') ) {
     2217                continue;
     2218            }
     2219
     2220            if ( preg_match( '/\stype=[\'"]hidden[\'"]\s/', $element ) ) {
     2221                echo $element;
     2222            }
     2223        }
     2224    }
    21882225    ?>
    21892226    <input type="hidden" id="user-id" name="user_ID" value="<?php echo (int) $user_id; ?>" />
     
    22032240    // Permalink title nonce.
    22042241    wp_nonce_field( 'samplepermalink', 'samplepermalinknonce', false );
    2205 }
     2242
     2243    /**
     2244     * Add hidden input fields to the meta box save form.
     2245     *
     2246     * Hook into this action to print `<input type="hidden" ... />` fields, which will be POSTed back to
     2247     * the server when meta boxes are saved.
     2248     *
     2249     * @since 5.0.0
     2250     *
     2251     * @params WP_Post $post The post that is being edited.
     2252     */
     2253    do_action( 'block_editor_meta_box_hidden_fields', $post );
     2254}
Note: See TracChangeset for help on using the changeset viewer.