wppaste
WordPress

meta_form( WP_Post $post = null )

Since
1.2.0
Source
wp-admin/includes/template.php:694
Prints the form in the Custom Fields meta box.

Compatibility

WordPress
since 1.2.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$postWP_Postoptional
The post being edited.Default: null

Performance profile

How much work a call to meta_form() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Heavy

Reaches the database via ->get_col().

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
50–96

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 122.

Plugin surface
2 hooks

Third-party callbacks on 'postmeta_form_keys', 'postmeta_form_limit' run inside this call, and their cost is not bounded by anything here.

Called by
1

1 place in core call this, so the cost is paid more often than your own code shows.

What it touches

  • querycontent queryget_post()called directly
  • hookthird-party callbacksapply_filters()called directly
  • sqldatabase query->get_col()called directly

Further down the call graph this can also reach option, cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 8 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost meta_form() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
$keys !== null50get_post(), apply_filters(), _e(), _ex(), _e(), wp_nonce_field(), __(), submit_button()
$keys !== null53get_post(), apply_filters(), natcasesort(), _e(), _ex(), _e(), wp_nonce_field(), __(), submit_button()
$keys !== null70–71get_post(), apply_filters(), _e(), _ex(), _e(), _e(), _e(), _e(), _e(), wp_nonce_field(), __(), submit_button()
$keys === null72get_post(), apply_filters(), apply_filters(), ->esc_like(), ->prepare(), ->get_col(), _e(), _ex(), _e(), wp_nonce_field(), __(), submit_button()
$keys !== null73–74get_post(), apply_filters(), natcasesort(), _e(), _ex(), _e(), _e(), _e(), _e(), _e(), wp_nonce_field(), __(), submit_button()
$keys === null75get_post(), apply_filters(), apply_filters(), ->esc_like(), ->prepare(), ->get_col(), natcasesort(), _e(), _ex(), _e(), wp_nonce_field(), __(), submit_button()
$keys === null92–93get_post(), apply_filters(), apply_filters(), ->esc_like(), ->prepare(), ->get_col(), _e(), _ex(), _e(), _e(), _e(), _e(), _e(), wp_nonce_field(), __(), submit_button()
$keys === null95–96get_post(), apply_filters(), apply_filters(), ->esc_like(), ->prepare(), ->get_col(), natcasesort(), _e(), _ex(), _e(), _e(), _e(), _e(), _e(), wp_nonce_field(), __(), submit_button()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 122 instructions, 50–96 executed per call, 7 branches. The work does not change between versions.

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 2

2 hooks fire while meta_form() runs, in this order:

  1. apply_filters( postmeta_form_keys )filterline 709 (+15 into the body)

    Filters values for the meta key dropdown in the Custom Fields meta box.

  2. apply_filters( postmeta_form_limit )filterline 720 (+26 into the body)

    Filters the number of custom fields to retrieve for the drop-down in the Custom Fields meta box.

Uses · 11

  • get_post()Retrieves post data given a post ID or post object.
  • apply_filters()Calls the callback functions that have been added to a filter hook.
  • _e()Displays translated text.
  • _ex()Displays translated string with gettext context.
  • is_protected_meta()Determines whether a meta key is considered protected.
  • current_user_can()Returns whether the current user has the specified capability.
  • esc_attr()Escaping for HTML attributes.
  • esc_html()Escaping for HTML blocks.
  • wp_nonce_field()Retrieves or display nonce hidden field for forms.
  • submit_button()Echoes a submit button, with provided text and appropriate class(es).
  • __()Retrieves the translation of $text.

Used by · 1

Source code

function meta_form( $post = null ) {	global $wpdb;	$post = get_post( $post ); 	/**	 * Filters values for the meta key dropdown in the Custom Fields meta box.	 *	 * Returning a non-null value will effectively short-circuit and avoid a	 * potentially expensive query against postmeta.	 *	 * @since 4.4.0	 *	 * @param array|null $keys Pre-defined meta keys to be used in place of a postmeta query. Default null.	 * @param WP_Post    $post The current post object.	 */	$keys = apply_filters( 'postmeta_form_keys', null, $post ); 	if ( null === $keys ) {		/**		 * Filters the number of custom fields to retrieve for the drop-down		 * in the Custom Fields meta box.		 *		 * @since 2.1.0		 *		 * @param int $limit Number of custom fields to retrieve. Default 30.		 */		$limit = apply_filters( 'postmeta_form_limit', 30 ); 		$keys = $wpdb->get_col(			$wpdb->prepare(				"SELECT DISTINCT meta_key				FROM $wpdb->postmeta				WHERE meta_key NOT BETWEEN '_' AND '_z'				HAVING meta_key NOT LIKE %s				ORDER BY meta_key				LIMIT %d",				$wpdb->esc_like( '_' ) . '%',				$limit			)		);	} 	if ( $keys ) {		natcasesort( $keys );	}	?><p><strong><?php _e( 'Add Custom Field:' ); ?></strong></p><table id="newmeta"><thead><tr><th class="left"><label for="metakeyselect"><?php _ex( 'Name', 'meta name' ); ?></label></th><th><label for="metavalue"><?php _e( 'Value' ); ?></label></th></tr></thead> <tbody><tr><td id="newmetaleft" class="left">	<?php if ( $keys ) { ?><select id="metakeyselect" name="metakeyselect"><option value="#NONE#"><?php _e( '&mdash; Select &mdash;' ); ?></option>		<?php		foreach ( $keys as $key ) {			if ( is_protected_meta( $key, 'post' ) || ! current_user_can( 'add_post_meta', $post->ID, $key ) ) {				continue;			}			echo "\n<option value='" . esc_attr( $key ) . "'>" . esc_html( $key ) . '</option>';		}		?></select><input class="hidden" type="text" id="metakeyinput" name="metakeyinput" value="" aria-label="<?php _e( 'New custom field name' ); ?>" /><button type="button" id="newmeta-button" class="button button-small hide-if-no-js" onclick="jQuery('#metakeyinput, #metakeyselect, #enternew, #cancelnew').toggleClass('hidden');jQuery('#metakeyinput, #metakeyselect').filter(':visible').trigger('focus');"><span id="enternew"><?php _e( 'Enter new' ); ?></span><span id="cancelnew" class="hidden"><?php _e( 'Cancel' ); ?></span></button><?php } else { ?><input type="text" id="metakeyinput" name="metakeyinput" value="" /><?php } ?></td><td><textarea id="metavalue" name="metavalue" rows="2" cols="25"></textarea>	<?php wp_nonce_field( 'add-meta', '_ajax_nonce-add-meta', false ); ?>

Changelog

Introduced in 1.2.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-admin/includes/template.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.