A WordPress-centric search engine for devs and theme authors



do_meta_boxes ›

Since2.5.0
Deprecatedn/a
do_meta_boxes ( $screen, $context, $object )
Parameters: (3)
  • (string|WP_Screen) $screen Screen identifier. If you have used add_menu_page() or add_submenu_page() to create a new screen (and hence screen_id) make sure your menu slug conforms to the limits of sanitize_key() otherwise the 'screen' menu may not correctly render on your page.
    Required: Yes
  • (string) $context The screen context for which to display meta boxes.
    Required: Yes
  • (mixed) $object Gets passed to the first parameter of the meta box callback function. Often this is the object that's the focus of the current screen, for example a `WP_Post` or `WP_Comment` object.
    Required: Yes
Returns:
  • (int) number of meta_boxes
Defined at:
Codex:

Displays the meta boxes which are registered against the given screen and context.



Source

function do_meta_boxes( $screen, $context, $object ) {
	global $wp_meta_boxes;
	static $already_sorted = false;

	if ( empty( $screen ) ) {
		$screen = get_current_screen();
	} elseif ( is_string( $screen ) ) {
		$screen = convert_to_screen( $screen );
	}

	$page = $screen->id;

	$hidden = get_hidden_meta_boxes( $screen );

	printf( '<div id="%s-sortables" class="meta-box-sortables">', esc_attr( $context ) );

	// Grab the ones the user has manually sorted. Pull them out of their previous context/priority and into the one the user chose
	if ( ! $already_sorted && $sorted = get_user_option( "meta-box-order_$page" ) ) {
		foreach ( $sorted as $box_context => $ids ) {
			foreach ( explode( ',', $ids ) as $id ) {
				if ( $id && 'dashboard_browser_nag' !== $id ) {
					add_meta_box( $id, null, null, $screen, $box_context, 'sorted' );
				}
			}
		}
	}

	$already_sorted = true;

	$i = 0;

	if ( isset( $wp_meta_boxes[ $page ][ $context ] ) ) {
		foreach ( array( 'high', 'sorted', 'core', 'default', 'low' ) as $priority ) {
			if ( isset( $wp_meta_boxes[ $page ][ $context ][ $priority ] ) ) {
				foreach ( (array) $wp_meta_boxes[ $page ][ $context ][ $priority ] as $box ) {
					if ( false == $box || ! $box['title'] ) {
						continue;
					}

					$block_compatible = true;
					if ( is_array( $box['args'] ) ) {
						// If a meta box is just here for back compat, don't show it in the block editor.
						if ( $screen->is_block_editor() && isset( $box['args']['__back_compat_meta_box'] ) && $box['args']['__back_compat_meta_box'] ) {
							continue;
						}

						// If a meta box doesn't work in the block editor, don't show it in the block editor.
						if ( $screen->is_block_editor() && isset( $box['args']['__block_editor_compatible_meta_box'] ) && ! $box['args']['__block_editor_compatible_meta_box'] ) {
							continue;
						}

						if ( isset( $box['args']['__block_editor_compatible_meta_box'] ) ) {
							$block_compatible = (bool) $box['args']['__block_editor_compatible_meta_box'];
							unset( $box['args']['__block_editor_compatible_meta_box'] );
						}

						if ( isset( $box['args']['__back_compat_meta_box'] ) ) {
							$block_compatible = $block_compatible || (bool) $box['args']['__back_compat_meta_box'];
							unset( $box['args']['__back_compat_meta_box'] );
						}
					}

					$i++;
					$hidden_class = in_array( $box['id'], $hidden ) ? ' hide-if-js' : '';
					echo '<div id="' . $box['id'] . '" class="postbox ' . postbox_classes( $box['id'], $page ) . $hidden_class . '" ' . '>' . "\n";
					if ( 'dashboard_browser_nag' != $box['id'] ) {
						$widget_title = $box['title'];

						if ( is_array( $box['args'] ) && isset( $box['args']['__widget_basename'] ) ) {
							$widget_title = $box['args']['__widget_basename'];
							// Do not pass this parameter to the user callback function.
							unset( $box['args']['__widget_basename'] );
						}

						echo '<button type="button" class="handlediv" aria-expanded="true">';
						echo '<span class="screen-reader-text">' . sprintf( __( 'Toggle panel: %s' ), $widget_title ) . '</span>';
						echo '<span class="toggle-indicator" aria-hidden="true"></span>';
						echo '</button>';
					}
					echo '<h2 class="hndle">';
					if ( 'dashboard_php_nag' === $box['id'] ) {
						echo '<span aria-hidden="true" class="dashicons dashicons-warning"></span>';
						echo '<span class="screen-reader-text">' . __( 'Warning:' ) . ' </span>';
					}
					echo "<span>{$box['title']}</span>";
					echo "</h2>\n";
					echo '<div class="inside">' . "\n";

					if ( WP_DEBUG && ! $block_compatible && 'edit' === $screen->parent_base && ! $screen->is_block_editor() && ! isset( $_GET['meta-box-loader'] ) ) {
						if ( is_array( $box['callback'] ) ) {
							$reflection = new ReflectionMethod( $box['callback'][0], $box['callback'][1] );
						} elseif ( false !== strpos( $box['callback'], '::' ) ) {
							$reflection = new ReflectionMethod( $box['callback'] );
						} else {
							$reflection = new ReflectionFunction( $box['callback'] );
						}

						// Don't show an error if it's an internal PHP function.
						if ( ! $reflection->isInternal() ) {

							// Only show errors if the meta box was registered by a plugin.
							$filename = $reflection->getFileName();
							if ( strpos( $filename, WP_PLUGIN_DIR ) === 0 ) {
								$filename = str_replace( WP_PLUGIN_DIR, '', $filename );
								$filename = preg_replace( '|^/([^/]*/).*$|', '\\1', $filename );

								$plugins = get_plugins();
								foreach ( $plugins as $name => $plugin ) {
									if ( strpos( $name, $filename ) === 0 ) {
										?>
											<div class="error inline">
												<p>
													<?php
														/* translators: %s: the name of the plugin that generated this meta box. */
														printf( __( "This meta box, from the %s plugin, isn't compatible with the block editor." ), "<strong>{$plugin['Name']}</strong>" );
													?>
												</p>
											</div>
										<?php
									}
								}
							}
						}
					}

					call_user_func( $box['callback'], $object, $box );
					echo "</div>\n";
					echo "</div>\n";
				}
			}
		}
	}

	echo '</div>';

	return $i;

}