Guest User

Untitled

a guest
May 25th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. <?php
  2.  
  3. $new_meta_boxes =
  4. array(
  5. "area" => array(
  6. "name" => "area",
  7. "type" => "input",
  8. "std" => "Unknown",
  9. "title" => "Area",
  10. "description" => "Where is the person located? (ex: Los Angeles, CA)"),
  11.  
  12. "age" => array(
  13. "name" => "age",
  14. "type" => "input",
  15. "std" => "Unknown",
  16. "title" => "Age",
  17. "description" => ""),
  18.  
  19. "country" => array(
  20. "name" => "country",
  21. "type" => "select",
  22. "std" => "USA",
  23. "title" => "Country",
  24. "options" => array("USA","Europe","Asia","Australia"),
  25. "description" => "")
  26. );
  27.  
  28. function new_meta_boxes() {
  29. global $post, $new_meta_boxes;
  30.  
  31. foreach($new_meta_boxes as $meta_box) {
  32. $meta_box_value = get_post_meta($post->ID, $meta_box['name'].'_value', true);
  33.  
  34. if($meta_box['type'] == "select") {
  35. echo'<h2>'.$meta_box['title'].'</h2>';
  36. echo'<select name="'.$meta_box['name'].'_noncename" id="'.$meta_box['name'].'_noncename" >';
  37. foreach ($value['options'] as $option) {
  38. echo'<option';
  39. if ( get_settings( $value['id'] ) == $option) {
  40. echo ' selected="selected"';
  41. } elseif ($option == $value['std']) {
  42. echo ' selected="selected"';
  43. }
  44.  
  45. echo '>'.$option.'</option>';
  46. }
  47.  
  48. echo'</select>';
  49. }
  50.  
  51.  
  52.  
  53. elseif($meta_box['type'] == "input")
  54. $meta_box_value = $meta_box['std'];
  55.  
  56. echo'<input type="hidden" name="'.$meta_box['name'].'_noncename" id="'.$meta_box['name'].'_noncename" value="'.wp_create_nonce( plugin_basename(__FILE__) ).'" />';
  57.  
  58. echo'<h2>'.$meta_box['title'].'</h2>';
  59. echo'<input type="text" name="'.$meta_box['name'].'_value" value="'.$meta_box_value.'" size="55" /><br />';
  60. echo'<p><label for="'.$meta_box['name'].'_value">'.$meta_box['description'].'</label></p>';
  61.  
  62. }
  63. }
  64.  
  65. function create_meta_box() {
  66. global $theme_name;
  67. if (function_exists('add_meta_box') ) {
  68. add_meta_box( 'new-meta-
  69. boxes', 'More Info', 'new_meta_boxes', 'post', 'normal', 'high' );
  70. }
  71. }
  72.  
  73. function save_postdata( $post_id ) {
  74. global $post, $new_meta_boxes;
  75. foreach($new_meta_boxes as $meta_box) {
  76.  
  77. // Verify
  78. if ( !wp_verify_nonce( $_POST[$meta_box['name'].'_noncename'], plugin_basename(__FILE__) )) {
  79. return $post_id;
  80. }
  81.  
  82. if ( 'page' == $_POST['post_type'] ) {
  83. if ( !current_user_can( 'edit_page', $post_id ))
  84. return $post_id;
  85. } else {
  86. if ( !current_user_can( 'edit_post', $post_id ))
  87. return $post_id;
  88. }
  89.  
  90. $data = $_POST[$meta_box['name'].'_value'];
  91.  
  92. if(get_post_meta($post_id, $meta_box['name'].'_value') == "")
  93. add_post_meta($post_id, $meta_box['name'].'_value', $data, true);
  94. elseif($data != get_post_meta($post_id, $meta_box['name'].'_value', true))
  95. update_post_meta($post_id, $meta_box['name'].'_value', $data);
  96. elseif($data == "")
  97. delete_post_meta($post_id, $meta_box['name'].'_value', get_post_meta($post_id, $meta_box['name'].'_value', true));
  98. }
  99. }
  100.  
  101. add_action('admin_menu', 'create_meta_box');
  102. add_action('save_post', 'save_postdata');
  103.  
  104. ?>
Add Comment
Please, Sign In to add comment