meta_form

The timeline below displays how wordpress function meta_form has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.3

/**
 * Prints the form in the Custom Fields meta box.
 *
 * @since 1.2.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param WP_Post $post Optional. The post being edited.
 */
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\n\t\t\t\tFROM {$wpdb->postmeta}\n\t\t\t\tWHERE meta_key NOT BETWEEN '_' AND '_z'\n\t\t\t\tHAVING meta_key NOT LIKE %s\n\t\t\t\tORDER BY meta_key\n\t\t\t\tLIMIT %d", $wpdb->esc_like('_') . '%', $limit));
    }
    if ($keys) {
        natcasesort($keys);
    }
    ?>
<p><strong><?php 
    _e('Add New 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);
    ?>
</td>
</tr>
</tbody>
</table>
<div class="submit add-custom-field">
	<?php 
    submit_button(__('Add Custom Field'), '', 'addmeta', false, array('id' => 'newmeta-submit', 'data-wp-lists' => 'add:the-list:newmeta'));
    ?>
</div>
	<?php 
}

WordPress Version: 5.5

/**
 * Prints the form in the Custom Fields meta box.
 *
 * @since 1.2.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param WP_Post $post Optional. The post being edited.
 */
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\n\t\t\t\tFROM {$wpdb->postmeta}\n\t\t\t\tWHERE meta_key NOT BETWEEN '_' AND '_z'\n\t\t\t\tHAVING meta_key NOT LIKE %s\n\t\t\t\tORDER BY meta_key\n\t\t\t\tLIMIT %d", $wpdb->esc_like('_') . '%', $limit));
    }
    if ($keys) {
        natcasesort($keys);
        $meta_key_input_id = 'metakeyselect';
    } else {
        $meta_key_input_id = 'metakeyinput';
    }
    ?>
<p><strong><?php 
    _e('Add New Custom Field:');
    ?></strong></p>
<table id="newmeta">
<thead>
<tr>
<th class="left"><label for="<?php 
    echo $meta_key_input_id;
    ?>"><?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="hide-if-js" type="text" id="metakeyinput" name="metakeyinput" value="" />
<a href="#postcustomstuff" class="hide-if-no-js" onclick="jQuery('#metakeyinput, #metakeyselect, #enternew, #cancelnew').toggle();return false;">
<span id="enternew"><?php 
        _e('Enter new');
        ?></span>
<span id="cancelnew" class="hidden"><?php 
        _e('Cancel');
        ?></span></a>
<?php 
    } else {
        ?>
<input type="text" id="metakeyinput" name="metakeyinput" value="" />
<?php 
    }
    ?>
</td>
<td><textarea id="metavalue" name="metavalue" rows="2" cols="25"></textarea></td>
</tr>

<tr><td colspan="2">
<div class="submit">
	<?php 
    submit_button(__('Add Custom Field'), '', 'addmeta', false, array('id' => 'newmeta-submit', 'data-wp-lists' => 'add:the-list:newmeta'));
    ?>
</div>
	<?php 
    wp_nonce_field('add-meta', '_ajax_nonce-add-meta', false);
    ?>
</td></tr>
</tbody>
</table>
	<?php 
}

WordPress Version: 5.1

/**
 * Prints the form in the Custom Fields meta box.
 *
 * @since 1.2.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param WP_Post $post Optional. The post being edited.
 */
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);
        $sql = "SELECT DISTINCT meta_key\n\t\t\tFROM {$wpdb->postmeta}\n\t\t\tWHERE meta_key NOT BETWEEN '_' AND '_z'\n\t\t\tHAVING meta_key NOT LIKE %s\n\t\t\tORDER BY meta_key\n\t\t\tLIMIT %d";
        $keys = $wpdb->get_col($wpdb->prepare($sql, $wpdb->esc_like('_') . '%', $limit));
    }
    if ($keys) {
        natcasesort($keys);
        $meta_key_input_id = 'metakeyselect';
    } else {
        $meta_key_input_id = 'metakeyinput';
    }
    ?>
<p><strong><?php 
    _e('Add New Custom Field:');
    ?></strong></p>
<table id="newmeta">
<thead>
<tr>
<th class="left"><label for="<?php 
    echo $meta_key_input_id;
    ?>"><?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="hide-if-js" type="text" id="metakeyinput" name="metakeyinput" value="" />
<a href="#postcustomstuff" class="hide-if-no-js" onclick="jQuery('#metakeyinput, #metakeyselect, #enternew, #cancelnew').toggle();return false;">
<span id="enternew"><?php 
        _e('Enter new');
        ?></span>
<span id="cancelnew" class="hidden"><?php 
        _e('Cancel');
        ?></span></a>
<?php 
    } else {
        ?>
<input type="text" id="metakeyinput" name="metakeyinput" value="" />
<?php 
    }
    ?>
</td>
<td><textarea id="metavalue" name="metavalue" rows="2" cols="25"></textarea></td>
</tr>

<tr><td colspan="2">
<div class="submit">
	<?php 
    submit_button(__('Add Custom Field'), '', 'addmeta', false, array('id' => 'newmeta-submit', 'data-wp-lists' => 'add:the-list:newmeta'));
    ?>
</div>
	<?php 
    wp_nonce_field('add-meta', '_ajax_nonce-add-meta', false);
    ?>
</td></tr>
</tbody>
</table>
	<?php 
}

WordPress Version: 4.7

/**
 * Prints the form in the Custom Fields meta box.
 *
 * @since 1.2.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param WP_Post $post Optional. The post being edited.
 */
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);
        $sql = "SELECT DISTINCT meta_key\n\t\t\tFROM {$wpdb->postmeta}\n\t\t\tWHERE meta_key NOT BETWEEN '_' AND '_z'\n\t\t\tHAVING meta_key NOT LIKE %s\n\t\t\tORDER BY meta_key\n\t\t\tLIMIT %d";
        $keys = $wpdb->get_col($wpdb->prepare($sql, $wpdb->esc_like('_') . '%', $limit));
    }
    if ($keys) {
        natcasesort($keys);
        $meta_key_input_id = 'metakeyselect';
    } else {
        $meta_key_input_id = 'metakeyinput';
    }
    ?>
<p><strong><?php 
    _e('Add New Custom Field:');
    ?></strong></p>
<table id="newmeta">
<thead>
<tr>
<th class="left"><label for="<?php 
    echo $meta_key_input_id;
    ?>"><?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="hide-if-js" type="text" id="metakeyinput" name="metakeyinput" value="" />
<a href="#postcustomstuff" class="hide-if-no-js" onclick="jQuery('#metakeyinput, #metakeyselect, #enternew, #cancelnew').toggle();return false;">
<span id="enternew"><?php 
        _e('Enter new');
        ?></span>
<span id="cancelnew" class="hidden"><?php 
        _e('Cancel');
        ?></span></a>
<?php 
    } else {
        ?>
<input type="text" id="metakeyinput" name="metakeyinput" value="" />
<?php 
    }
    ?>
</td>
<td><textarea id="metavalue" name="metavalue" rows="2" cols="25"></textarea></td>
</tr>

<tr><td colspan="2">
<div class="submit">
<?php 
    submit_button(__('Add Custom Field'), '', 'addmeta', false, array('id' => 'newmeta-submit', 'data-wp-lists' => 'add:the-list:newmeta'));
    ?>
</div>
<?php 
    wp_nonce_field('add-meta', '_ajax_nonce-add-meta', false);
    ?>
</td></tr>
</tbody>
</table>
<?php 
}

WordPress Version: 4.6

/**
 * Prints the form in the Custom Fields meta box.
 *
 * @since 1.2.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param WP_Post $post Optional. The post being edited.
 */
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);
        $sql = "SELECT DISTINCT meta_key\n\t\t\tFROM {$wpdb->postmeta}\n\t\t\tWHERE meta_key NOT BETWEEN '_' AND '_z'\n\t\t\tHAVING meta_key NOT LIKE %s\n\t\t\tORDER BY meta_key\n\t\t\tLIMIT %d";
        $keys = $wpdb->get_col($wpdb->prepare($sql, $wpdb->esc_like('_') . '%', $limit));
    }
    if ($keys) {
        natcasesort($keys);
        $meta_key_input_id = 'metakeyselect';
    } else {
        $meta_key_input_id = 'metakeyinput';
    }
    ?>
<p><strong><?php 
    _e('Add New Custom Field:');
    ?></strong></p>
<table id="newmeta">
<thead>
<tr>
<th class="left"><label for="<?php 
    echo $meta_key_input_id;
    ?>"><?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="hide-if-js" type="text" id="metakeyinput" name="metakeyinput" value="" />
<a href="#postcustomstuff" class="hide-if-no-js" onclick="jQuery('#metakeyinput, #metakeyselect, #enternew, #cancelnew').toggle();return false;">
<span id="enternew"><?php 
        _e('Enter new');
        ?></span>
<span id="cancelnew" class="hidden"><?php 
        _e('Cancel');
        ?></span></a>
<?php 
    } else {
        ?>
<input type="text" id="metakeyinput" name="metakeyinput" value="" />
<?php 
    }
    ?>
</td>
<td><textarea id="metavalue" name="metavalue" rows="2" cols="25"></textarea></td>
</tr>

<tr><td colspan="2">
<div class="submit">
<?php 
    submit_button(__('Add Custom Field'), 'secondary', 'addmeta', false, array('id' => 'newmeta-submit', 'data-wp-lists' => 'add:the-list:newmeta'));
    ?>
</div>
<?php 
    wp_nonce_field('add-meta', '_ajax_nonce-add-meta', false);
    ?>
</td></tr>
</tbody>
</table>
<?php 
}

WordPress Version: 4.4

/**
 * Prints the form in the Custom Fields meta box.
 *
 * @since 1.2.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param WP_Post $post Optional. The post being edited.
 */
function meta_form($post = null)
{
    global $wpdb;
    $post = get_post($post);
    /**
     * Filter 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) {
        /**
         * Filter 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);
        $sql = "SELECT DISTINCT meta_key\n\t\t\tFROM {$wpdb->postmeta}\n\t\t\tWHERE meta_key NOT BETWEEN '_' AND '_z'\n\t\t\tHAVING meta_key NOT LIKE %s\n\t\t\tORDER BY meta_key\n\t\t\tLIMIT %d";
        $keys = $wpdb->get_col($wpdb->prepare($sql, $wpdb->esc_like('_') . '%', $limit));
    }
    if ($keys) {
        natcasesort($keys);
        $meta_key_input_id = 'metakeyselect';
    } else {
        $meta_key_input_id = 'metakeyinput';
    }
    ?>
<p><strong><?php 
    _e('Add New Custom Field:');
    ?></strong></p>
<table id="newmeta">
<thead>
<tr>
<th class="left"><label for="<?php 
    echo $meta_key_input_id;
    ?>"><?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="hide-if-js" type="text" id="metakeyinput" name="metakeyinput" value="" />
<a href="#postcustomstuff" class="hide-if-no-js" onclick="jQuery('#metakeyinput, #metakeyselect, #enternew, #cancelnew').toggle();return false;">
<span id="enternew"><?php 
        _e('Enter new');
        ?></span>
<span id="cancelnew" class="hidden"><?php 
        _e('Cancel');
        ?></span></a>
<?php 
    } else {
        ?>
<input type="text" id="metakeyinput" name="metakeyinput" value="" />
<?php 
    }
    ?>
</td>
<td><textarea id="metavalue" name="metavalue" rows="2" cols="25"></textarea></td>
</tr>

<tr><td colspan="2">
<div class="submit">
<?php 
    submit_button(__('Add Custom Field'), 'secondary', 'addmeta', false, array('id' => 'newmeta-submit', 'data-wp-lists' => 'add:the-list:newmeta'));
    ?>
</div>
<?php 
    wp_nonce_field('add-meta', '_ajax_nonce-add-meta', false);
    ?>
</td></tr>
</tbody>
</table>
<?php 
}

WordPress Version: 4.3

/**
 * Prints the form in the Custom Fields meta box.
 *
 * @since 1.2.0
 *
 * @global wpdb $wpdb
 *
 * @param WP_Post $post Optional. The post being edited.
 */
function meta_form($post = null)
{
    global $wpdb;
    $post = get_post($post);
    /**
     * Filter 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);
    $sql = "SELECT DISTINCT meta_key\n\t\tFROM {$wpdb->postmeta}\n\t\tWHERE meta_key NOT BETWEEN '_' AND '_z'\n\t\tHAVING meta_key NOT LIKE %s\n\t\tORDER BY meta_key\n\t\tLIMIT %d";
    $keys = $wpdb->get_col($wpdb->prepare($sql, $wpdb->esc_like('_') . '%', $limit));
    if ($keys) {
        natcasesort($keys);
        $meta_key_input_id = 'metakeyselect';
    } else {
        $meta_key_input_id = 'metakeyinput';
    }
    ?>
<p><strong><?php 
    _e('Add New Custom Field:');
    ?></strong></p>
<table id="newmeta">
<thead>
<tr>
<th class="left"><label for="<?php 
    echo $meta_key_input_id;
    ?>"><?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="hide-if-js" type="text" id="metakeyinput" name="metakeyinput" value="" />
<a href="#postcustomstuff" class="hide-if-no-js" onclick="jQuery('#metakeyinput, #metakeyselect, #enternew, #cancelnew').toggle();return false;">
<span id="enternew"><?php 
        _e('Enter new');
        ?></span>
<span id="cancelnew" class="hidden"><?php 
        _e('Cancel');
        ?></span></a>
<?php 
    } else {
        ?>
<input type="text" id="metakeyinput" name="metakeyinput" value="" />
<?php 
    }
    ?>
</td>
<td><textarea id="metavalue" name="metavalue" rows="2" cols="25"></textarea></td>
</tr>

<tr><td colspan="2">
<div class="submit">
<?php 
    submit_button(__('Add Custom Field'), 'secondary', 'addmeta', false, array('id' => 'newmeta-submit', 'data-wp-lists' => 'add:the-list:newmeta'));
    ?>
</div>
<?php 
    wp_nonce_field('add-meta', '_ajax_nonce-add-meta', false);
    ?>
</td></tr>
</tbody>
</table>
<?php 
}

WordPress Version: 4.0

/**
 * Prints the form in the Custom Fields meta box.
 *
 * @since 1.2.0
 *
 * @param WP_Post $post Optional. The post being edited.
 */
function meta_form($post = null)
{
    global $wpdb;
    $post = get_post($post);
    /**
     * Filter 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);
    $sql = "SELECT meta_key\n\t\tFROM {$wpdb->postmeta}\n\t\tGROUP BY meta_key\n\t\tHAVING meta_key NOT LIKE %s\n\t\tORDER BY meta_key\n\t\tLIMIT %d";
    $keys = $wpdb->get_col($wpdb->prepare($sql, $wpdb->esc_like('_') . '%', $limit));
    if ($keys) {
        natcasesort($keys);
        $meta_key_input_id = 'metakeyselect';
    } else {
        $meta_key_input_id = 'metakeyinput';
    }
    ?>
<p><strong><?php 
    _e('Add New Custom Field:');
    ?></strong></p>
<table id="newmeta">
<thead>
<tr>
<th class="left"><label for="<?php 
    echo $meta_key_input_id;
    ?>"><?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="hide-if-js" type="text" id="metakeyinput" name="metakeyinput" value="" />
<a href="#postcustomstuff" class="hide-if-no-js" onclick="jQuery('#metakeyinput, #metakeyselect, #enternew, #cancelnew').toggle();return false;">
<span id="enternew"><?php 
        _e('Enter new');
        ?></span>
<span id="cancelnew" class="hidden"><?php 
        _e('Cancel');
        ?></span></a>
<?php 
    } else {
        ?>
<input type="text" id="metakeyinput" name="metakeyinput" value="" />
<?php 
    }
    ?>
</td>
<td><textarea id="metavalue" name="metavalue" rows="2" cols="25"></textarea></td>
</tr>

<tr><td colspan="2">
<div class="submit">
<?php 
    submit_button(__('Add Custom Field'), 'secondary', 'addmeta', false, array('id' => 'newmeta-submit', 'data-wp-lists' => 'add:the-list:newmeta'));
    ?>
</div>
<?php 
    wp_nonce_field('add-meta', '_ajax_nonce-add-meta', false);
    ?>
</td></tr>
</tbody>
</table>
<?php 
}

WordPress Version: 3.9

/**
 * Prints the form in the Custom Fields meta box.
 *
 * @since 1.2.0
 *
 * @param WP_Post $post Optional. The post being edited.
 */
function meta_form($post = null)
{
    global $wpdb;
    $post = get_post($post);
    /**
     * Filter 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 = (int) apply_filters('postmeta_form_limit', 30);
    $keys = $wpdb->get_col("\n\t\tSELECT meta_key\n\t\tFROM {$wpdb->postmeta}\n\t\tGROUP BY meta_key\n\t\tHAVING meta_key NOT LIKE '\\_%'\n\t\tORDER BY meta_key\n\t\tLIMIT {$limit}");
    if ($keys) {
        natcasesort($keys);
        $meta_key_input_id = 'metakeyselect';
    } else {
        $meta_key_input_id = 'metakeyinput';
    }
    ?>
<p><strong><?php 
    _e('Add New Custom Field:');
    ?></strong></p>
<table id="newmeta">
<thead>
<tr>
<th class="left"><label for="<?php 
    echo $meta_key_input_id;
    ?>"><?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="hide-if-js" type="text" id="metakeyinput" name="metakeyinput" value="" />
<a href="#postcustomstuff" class="hide-if-no-js" onclick="jQuery('#metakeyinput, #metakeyselect, #enternew, #cancelnew').toggle();return false;">
<span id="enternew"><?php 
        _e('Enter new');
        ?></span>
<span id="cancelnew" class="hidden"><?php 
        _e('Cancel');
        ?></span></a>
<?php 
    } else {
        ?>
<input type="text" id="metakeyinput" name="metakeyinput" value="" />
<?php 
    }
    ?>
</td>
<td><textarea id="metavalue" name="metavalue" rows="2" cols="25"></textarea></td>
</tr>

<tr><td colspan="2">
<div class="submit">
<?php 
    submit_button(__('Add Custom Field'), 'secondary', 'addmeta', false, array('id' => 'newmeta-submit', 'data-wp-lists' => 'add:the-list:newmeta'));
    ?>
</div>
<?php 
    wp_nonce_field('add-meta', '_ajax_nonce-add-meta', false);
    ?>
</td></tr>
</tbody>
</table>
<?php 
}

WordPress Version: 3.7

/**
 * Prints the form in the Custom Fields meta box.
 *
 * @since 1.2.0
 *
 * @param WP_Post $post Optional. The post being edited.
 */
function meta_form($post = null)
{
    global $wpdb;
    $post = get_post($post);
    $limit = (int) apply_filters('postmeta_form_limit', 30);
    $keys = $wpdb->get_col("\n\t\tSELECT meta_key\n\t\tFROM {$wpdb->postmeta}\n\t\tGROUP BY meta_key\n\t\tHAVING meta_key NOT LIKE '\\_%'\n\t\tORDER BY meta_key\n\t\tLIMIT {$limit}");
    if ($keys) {
        natcasesort($keys);
    }
    ?>
<p><strong><?php 
    _e('Add New 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="hide-if-js" type="text" id="metakeyinput" name="metakeyinput" value="" />
<a href="#postcustomstuff" class="hide-if-no-js" onclick="jQuery('#metakeyinput, #metakeyselect, #enternew, #cancelnew').toggle();return false;">
<span id="enternew"><?php 
        _e('Enter new');
        ?></span>
<span id="cancelnew" class="hidden"><?php 
        _e('Cancel');
        ?></span></a>
<?php 
    } else {
        ?>
<input type="text" id="metakeyinput" name="metakeyinput" value="" />
<?php 
    }
    ?>
</td>
<td><textarea id="metavalue" name="metavalue" rows="2" cols="25"></textarea></td>
</tr>

<tr><td colspan="2">
<div class="submit">
<?php 
    submit_button(__('Add Custom Field'), 'secondary', 'addmeta', false, array('id' => 'newmeta-submit', 'data-wp-lists' => 'add:the-list:newmeta'));
    ?>
</div>
<?php 
    wp_nonce_field('add-meta', '_ajax_nonce-add-meta', false);
    ?>
</td></tr>
</tbody>
</table>
<?php 
}