Advertisement
Guest User

Untitled

a guest
Oct 9th, 2019
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.14 KB | None | 0 0
  1. <?php
  2. function custom_meta_box_markup($object)
  3. {
  4. wp_nonce_field(basename(__FILE__), "meta-box-nonce");
  5.  
  6. ?>
  7. <div>
  8. <label for="meta-box-text">Text</label>
  9. <input name="meta-box-text" type="text" value="<?php echo get_post_meta($object->ID, "meta-box-text", true); ?>">
  10.  
  11. <br>
  12.  
  13. <label for="meta-box-dropdown">Dropdown</label>
  14. <select name="meta-box-dropdown">
  15. <?php
  16. $option_values = array(1, 2, 3);
  17.  
  18. foreach($option_values as $key => $value)
  19. {
  20. if($value == get_post_meta($object->ID, "meta-box-dropdown", true))
  21. {
  22. ?>
  23. <option selected><?php echo $value; ?></option>
  24. <?php
  25. }
  26. else
  27. {
  28. ?>
  29. <option><?php echo $value; ?></option>
  30. <?php
  31. }
  32. }
  33. ?>
  34. </select>
  35.  
  36. <br>
  37.  
  38. <label for="meta-box-checkbox">Check Box</label>
  39. <?php
  40. $checkbox_value = get_post_meta($object->ID, "meta-box-checkbox", true);
  41.  
  42. if($checkbox_value == "")
  43. {
  44. ?>
  45. <input name="meta-box-checkbox" type="checkbox" value="true">
  46. <?php
  47. }
  48. else if($checkbox_value == "true")
  49. {
  50. ?>
  51. <input name="meta-box-checkbox" type="checkbox" value="true" checked>
  52. <?php
  53. }
  54. ?>
  55. </div>
  56. <?php
  57. }
  58.  
  59. function add_custom_meta_box()
  60. {
  61. add_meta_box("demo-meta-box", "Custom Meta Box", "custom_meta_box_markup", "post", "side", "high", null);
  62. }
  63.  
  64. add_action("add_meta_boxes", "add_custom_meta_box");
  65.  
  66. function save_custom_meta_box($post_id, $post, $update)
  67. {
  68. if (!isset($_POST["meta-box-nonce"]) || !wp_verify_nonce($_POST["meta-box-nonce"], basename(__FILE__)))
  69. return $post_id;
  70.  
  71. if(!current_user_can("edit_post", $post_id))
  72. return $post_id;
  73.  
  74. if(defined("DOING_AUTOSAVE") && DOING_AUTOSAVE)
  75. return $post_id;
  76.  
  77. $slug = "post";
  78. if($slug != $post->post_type)
  79. return $post_id;
  80.  
  81. $meta_box_text_value = "";
  82. $meta_box_dropdown_value = "";
  83. $meta_box_checkbox_value = "";
  84.  
  85. if(isset($_POST["meta-box-text"]))
  86. {
  87. $meta_box_text_value = $_POST["meta-box-text"];
  88. }
  89. update_post_meta($post_id, "meta-box-text", $meta_box_text_value);
  90.  
  91. if(isset($_POST["meta-box-dropdown"]))
  92. {
  93. $meta_box_dropdown_value = $_POST["meta-box-dropdown"];
  94. }
  95. update_post_meta($post_id, "meta-box-dropdown", $meta_box_dropdown_value);
  96.  
  97. if(isset($_POST["meta-box-checkbox"]))
  98. {
  99. $meta_box_checkbox_value = $_POST["meta-box-checkbox"];
  100. }
  101. update_post_meta($post_id, "meta-box-checkbox", $meta_box_checkbox_value);
  102. }
  103.  
  104. add_action("save_post", "save_custom_meta_box", 10, 3);
  105. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement