Advertisement
Guest User

Untitled

a guest
Oct 21st, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.67 KB | None | 0 0
  1. function room_facility_custom_meta() {
  2. add_meta_box( 'room_facility_meta', __( 'Meta Box ', 'room_facility-textdomain' ), 'room_facility_meta_callback', 'room_type' );
  3. }
  4. add_action( 'add_meta_boxes', 'room_facility_custom_meta' );
  5.  
  6. function room_facility_meta_callback( $post ) {
  7. //nonce_field
  8. wp_nonce_field( basename( __FILE__ ), 'room_facility_nonce' );
  9.  
  10. $tax = "room_facility";
  11. $term_facilities = get_terms( $tax, array('hide_empty' => false));
  12. $term_checked_facilities= wp_get_post_terms($post->ID, $tax);
  13.  
  14. foreach($term_facilities as $term){
  15.  
  16. $room_facility_id = $term->term_id;
  17. /* the meta_key for postmeta, note there is the room_facility included,
  18. * since there should be a relationship between post_id (in this case from taxonomy "room_type")
  19. * and "room_facility"
  20. */
  21. $meta_key = "_room_facility_key_".$room_facility_id;
  22. //value from the postmeta
  23. $room_facility_value = get_post_meta($post->ID, $meta_key);
  24.  
  25. ?>
  26. <div class="row">
  27. <label for="checkbox_<?php echo $room_facility_id ?>">
  28. <input type="checkbox" name="checkbox_<?php echo $room_facility_id ?>" id="checkbox_<?php echo $room_facility_id ?>" <?php checked( myArrayContainsWord($term_checked_facilities, $term->term_id)); ?> />
  29. <?php _e( $term->name, 'prfx-textdomain' )?>
  30. </label>
  31. <input type="text" name="text_<?php echo $room_facility_id ?>" id="text_<?php echo $room_facility_id ?>" value="<?php if ( !empty ( $room_facility_value[0] ) ){ echo $room_facility_value[0]; }?>" />
  32. </div>
  33. <?php
  34. } //end foreach
  35. }
  36.  
  37. function room_facility_meta_save( $post_id )
  38. {
  39. // Verify this came from the our screen and with proper authorization,
  40. // because save_post can be triggered at other times
  41. //if ( !wp_verify_nonce( $_POST['room_facility_nonce'], plugin_basename(__FILE__) )) { <-- THIS DIDN'T WORK
  42. if(! isset( $_POST['room_facility_nonce'] )){
  43. return $post_id;
  44. }
  45.  
  46. // Verify if this is an auto save routine. If it is our form has not been submitted, so we dont want
  47. // to do anything
  48.  
  49.  
  50. if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
  51. return $post_id;
  52.  
  53.  
  54. //wp_set_object_terms( 742, array(211), 'room_facility');
  55. $taxonomy = "room_facility";
  56. $term_facilities = get_terms( $taxonomy, array('hide_empty' => false));
  57.  
  58. foreach ($term_facilities as $tax => $term)
  59. {
  60. $room_facility_id = $term->term_id;
  61.  
  62. /* the meta_key for postmeta, note there is the room_facility included,
  63. * since there should be a relationship between post_id (in this case from taxonomy "room_type")
  64. * and "room_facility"
  65. */
  66. $meta_key = "_room_facility_key_".$room_facility_id;
  67.  
  68. //update postmeta room_facility_key_<ID>
  69. if( isset( $_POST[ "text_".$room_facility_id] ) ) {
  70. update_post_meta( $post_id, $meta_key, sanitize_text_field( $_POST[ "text_".$room_facility_id] ) );
  71. }
  72.  
  73. //set wp_term_relationships for room_type and room_facility
  74. if( isset( $_POST[ 'checkbox_'.$room_facility_id ] ) )
  75. {
  76. //wp_set_post_terms( $post_id, $term, $taxonomy); <-- WRONG function
  77. wp_set_object_terms( $post_id, array($term->term_id) , $taxonomy);
  78. } /*else {
  79. wp_set_post_terms( $post_id, $term, $taxonomy);
  80. }*/
  81. }
  82. }
  83. add_action( 'save_post', 'room_facility_meta_save' );
  84.  
  85. echo '<script>alert("test")</script>';
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement