Advertisement
Guest User

Untitled

a guest
Jul 6th, 2015
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.68 KB | None | 0 0
  1. <?php
  2.  
  3. function wptrt_add_meta_box() {
  4. add_meta_box( 'wptrt-sample-meta-box', esc_html__( 'WPTRT Sample Meta Box', 'wptrt' ), 'wptrt_print_meta_box', 'post' );
  5. }
  6. add_action( 'add_meta_boxes', 'wptrt_add_meta_box' );
  7.  
  8. function wptrt_print_meta_box() {
  9. wp_nonce_field( 'wptrt-post-meta-box-save', 'wptrt-post-meta-box-nonce' );
  10. ?>
  11. <p>
  12. <input type="checkbox" id="wptrt-individual-checkbox" name="wptrt-individual-checkbox" value="1" <?php checked( get_post_meta( get_the_ID(), 'wptrt-individual-checkbox', true ) ); ?> />
  13. <label for="wptrt-individual-checkbox"><?php echo esc_html__( 'Individual Checkbox', 'wptrt' ); ?></label>
  14. </p>
  15.  
  16. <p>
  17. <label for="wptrt-individual-text-field"><?php echo esc_html__( 'Individual Text Field', 'wptrt' ); ?></label>
  18. <input type="text" id="wptrt-individual-text-field" name="wptrt-individual-text-field" value="<?php echo esc_attr( get_post_meta( get_the_ID(), 'wptrt-individual-text-field', true ) ); ?>" />
  19. </p>
  20.  
  21. <?php $hide_elements = (array) get_post_meta( get_the_ID(), 'wptrt-hide-post-element', true ); ?>
  22. <p>
  23. <input type="checkbox" id="wptrt-hide-post-date" name="wptrt-hide-post-element[]" value="date" <?php checked( in_array( 'date', $hide_elements, true ) ); ?> />
  24. <label for="wptrt-hide-post-date"><?php echo esc_html__( 'Hide Date', 'wptrt' ); ?></label>
  25. </p>
  26. <p>
  27. <input type="checkbox" id="wptrt-hide-post-author" name="wptrt-hide-post-element[]" value="author" <?php checked( in_array( 'author', $hide_elements, true ) ); ?> />
  28. <label for="wptrt-hide-post-author"><?php echo esc_html__( 'Hide Author', 'wptrt' ); ?></label>
  29. </p>
  30. <p>
  31. <input type="checkbox" id="wptrt-hide-post-categories" name="wptrt-hide-post-element[]" value="categories" <?php checked( in_array( 'categories', $hide_elements, true ) ); ?> />
  32. <label for="wptrt-hide-post-categories"><?php echo esc_html__( 'Hide Categories', 'wptrt' ); ?></label>
  33. </p>
  34.  
  35. <?php
  36. $favorite_colors = (array) get_post_meta( get_the_ID(), 'wptrt-favorite-color', true );
  37. foreach ( wptrt_get_favorite_color_options() as $option => $text ) :
  38. ?>
  39. <p>
  40. <input type="checkbox" id="wptrt-favorite-color-<?php echo esc_attr( $option ); ?>" name="wptrt-favorite-color[]" value="<?php echo esc_attr( $option ); ?>" <?php checked( in_array( $option, $favorite_colors, true ) ); ?> />
  41. <label for="wptrt-favorite-color-<?php echo esc_attr( $option ); ?>"><?php echo esc_html( $text ); ?></label>
  42. </p>
  43. <?php endforeach;
  44. }
  45.  
  46. function wptrt_save_meta_box_data( $post_id ) {
  47. if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
  48. return;
  49. }
  50.  
  51. if ( ! isset( $_POST['wptrt-post-meta-box-nonce'] ) && ! wp_verify_nonce( $_POST['wptrt-post-meta-box-nonce'] ) ) {
  52. return;
  53. }
  54.  
  55. if ( ! current_user_can( 'edit_post', $post_id ) ) {
  56. return;
  57. }
  58.  
  59. // Individual checkbox.
  60. if ( ! isset( $_POST['wptrt-individual-checkbox'] ) && get_post_meta( $post_id, 'wptrt-individual-checkbox', true ) ) {
  61. delete_post_meta( $post_id, 'wptrt-individual-checkbox' );
  62. } else {
  63. update_post_meta( $post_id, 'wptrt-individual-checkbox', 1 );
  64. }
  65.  
  66. // Individual text field.
  67. if ( empty( $_POST['wptrt-individual-text-field'] ) ) {
  68. if ( get_post_meta( $post_id, 'wptrt-individual-text-field', true ) ) {
  69. delete_post_meta( $post_id, 'wptrt-individual-text-field' );
  70. }
  71. } else {
  72. update_post_meta( $post_id, 'wptrt-individual-text-field', sanitize_text_field( $_POST['wptrt-individual-text-field'] ) );
  73. }
  74.  
  75. // Grouped checkboxes
  76. if ( ! isset( $_POST['wptrt-hide-post-element'] ) ) {
  77. if ( get_post_meta( $post_id, 'wptrt-hide-post-element', true ) ) {
  78. delete_post_meta( $post_id, 'wptrt-hide-post-element' );
  79. }
  80. } else {
  81. $safe_hide_post_element = array();
  82.  
  83. foreach ( $_POST['wptrt-hide-post-element'] as $element ) {
  84. if ( in_array( $element, array( 'date', 'author', 'categories' ), true ) ) {
  85. $safe_hide_post_element[] = $element;
  86. }
  87. }
  88.  
  89. if ( ! empty( $safe_hide_post_element ) ) {
  90. update_post_meta( $post_id, 'wptrt-hide-post-element', $safe_hide_post_element );
  91. }
  92. }
  93.  
  94. // Favorite color
  95. if ( ! isset( $_POST['wptrt-favorite-color'] ) ) {
  96. if ( get_post_meta( $post_id, 'wptrt-favorite-color', true ) ) {
  97. delete_post_meta( $post_id, 'wptrt-favorite-color' );
  98. }
  99. } else {
  100. $safe_favorite_color = array();
  101.  
  102. foreach ( $_POST['wptrt-favorite-color'] as $color ) {
  103. if ( array_key_exists( $color, wptrt_get_favorite_color_options() ) ) {
  104. $safe_favorite_color[] = $color;
  105. }
  106. }
  107.  
  108. if ( ! empty( $safe_favorite_color ) ) {
  109. update_post_meta( $post_id, 'wptrt-favorite-color', $safe_favorite_color );
  110. }
  111. }
  112. }
  113. add_action( 'save_post', 'wptrt_save_meta_box_data' );
  114.  
  115. function wptrt_get_favorite_color_options() {
  116. return array(
  117. 'blue' => __( 'Blue', 'wptrt' ),
  118. 'red' => __( 'Red', 'wptrt' ),
  119. 'yellow' => __( 'Yellow', 'wptrt' ),
  120. );
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement