meta_form

Prints the form in the Custom Fields meta box.

Description

meta_form( (null) $post = null ); 

Parameters (1)

0. $post — Optional. (null) => null
The post being edited.

Usage

  1. if ( !function_exists( 'meta_form' ) ) { 
  2. require_once ABSPATH . '/wp-admin/includes/template.php'; 
  3.  
  4. // Optional. The post being edited. 
  5. $post = null; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = meta_form($post); 
  9.  

Defined (1)

The function is defined in the following location(s).

/wp-admin/includes/template.php  
  1. function meta_form( $post = null ) { 
  2. global $wpdb; 
  3. $post = get_post( $post ); 
  4.  
  5. /** 
  6. * Filters values for the meta key dropdown in the Custom Fields meta box. 
  7. * Returning a non-null value will effectively short-circuit and avoid a 
  8. * potentially expensive query against postmeta. 
  9. * @since 4.4.0 
  10. * @param array|null $keys Pre-defined meta keys to be used in place of a postmeta query. Default null. 
  11. * @param WP_Post $post The current post object. 
  12. */ 
  13. $keys = apply_filters( 'postmeta_form_keys', null, $post ); 
  14.  
  15. if ( null === $keys ) { 
  16. /** 
  17. * Filters the number of custom fields to retrieve for the drop-down 
  18. * in the Custom Fields meta box. 
  19. * @since 2.1.0 
  20. * @param int $limit Number of custom fields to retrieve. Default 30. 
  21. */ 
  22. $sql = "SELECT DISTINCT meta_key 
  23. FROM $wpdb->postmeta 
  24. WHERE meta_key NOT BETWEEN '_' AND '_z' 
  25. HAVING meta_key NOT LIKE %s 
  26. ORDER BY meta_key 
  27. LIMIT %d"; 
  28. $keys = $wpdb->get_col( $wpdb->prepare( $sql, $wpdb->esc_like( '_' ) . '%', $limit ) ); 
  29.  
  30. if ( $keys ) { 
  31. natcasesort( $keys ); 
  32. $meta_key_input_id = 'metakeyselect'; 
  33. } else { 
  34. $meta_key_input_id = 'metakeyinput'; 
  35. ?> 
  36. <p><strong><?php _e( 'Add New Custom Field:' ) ?></strong></p> 
  37. <table id="newmeta"> 
  38. <thead> 
  39. <tr> 
  40. <th class="left"><label for="<?php echo $meta_key_input_id; ?>"><?php _ex( 'Name', 'meta name' ) ?></label></th> 
  41. <th><label for="metavalue"><?php _e( 'Value' ) ?></label></th> 
  42. </tr> 
  43. </thead> 
  44.  
  45. <tbody> 
  46. <tr> 
  47. <td id="newmetaleft" class="left"> 
  48. <?php if ( $keys ) { ?> 
  49. <select id="metakeyselect" name="metakeyselect"> 
  50. <option value="#NONE#"><?php _e( '— Select —' ); ?></option> 
  51. <?php 
  52.  
  53. foreach ( $keys as $key ) { 
  54. if ( is_protected_meta( $key, 'post' ) || ! current_user_can( 'add_post_meta', $post->ID, $key ) ) 
  55. continue; 
  56. echo "\n<option value='" . esc_attr($key) . "'>" . esc_html($key) . "</option>"; 
  57. ?> 
  58. </select> 
  59. <input class="hide-if-js" type="text" id="metakeyinput" name="metakeyinput" value="" /> 
  60. <a href="#postcustomstuff" class="hide-if-no-js" onclick="jQuery('#metakeyinput, #metakeyselect, #enternew, #cancelnew').toggle();return false;"> 
  61. <span id="enternew"><?php _e('Enter new'); ?></span> 
  62. <span id="cancelnew" class="hidden"><?php _e('Cancel'); ?></span></a> 
  63. <?php } else { ?> 
  64. <input type="text" id="metakeyinput" name="metakeyinput" value="" /> 
  65. <?php } ?> 
  66. </td> 
  67. <td><textarea id="metavalue" name="metavalue" rows="2" cols="25"></textarea></td> 
  68. </tr> 
  69.  
  70. <tr><td colspan="2"> 
  71. <div class="submit"> 
  72. <?php submit_button( __( 'Add Custom Field' ), '', 'addmeta', false, array( 'id' => 'newmeta-submit', 'data-wp-lists' => 'add:the-list:newmeta' ) ); ?> 
  73. </div> 
  74. <?php wp_nonce_field( 'add-meta', '_ajax_nonce-add-meta', false ); ?> 
  75. </td></tr> 
  76. </tbody> 
  77. </table> 
  78. <?php 
  79.