Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function room_facility_custom_meta() {
- add_meta_box( 'room_facility_meta', __( 'Meta Box ', 'room_facility-textdomain' ), 'room_facility_meta_callback', 'room_type' );
- }
- add_action( 'add_meta_boxes', 'room_facility_custom_meta' );
- function room_facility_meta_callback( $post ) {
- //nonce_field
- wp_nonce_field( basename( __FILE__ ), 'room_facility_nonce' );
- $tax = "room_facility";
- $term_facilities = get_terms( $tax, array('hide_empty' => false));
- $term_checked_facilities= wp_get_post_terms($post->ID, $tax);
- foreach($term_facilities as $term){
- $room_facility_id = $term->term_id;
- /* the meta_key for postmeta, note there is the room_facility included,
- * since there should be a relationship between post_id (in this case from taxonomy "room_type")
- * and "room_facility"
- */
- $meta_key = "_room_facility_key_".$room_facility_id;
- //value from the postmeta
- $room_facility_value = get_post_meta($post->ID, $meta_key);
- ?>
- <div class="row">
- <label for="checkbox_<?php echo $room_facility_id ?>">
- <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)); ?> />
- <?php _e( $term->name, 'prfx-textdomain' )?>
- </label>
- <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]; }?>" />
- </div>
- <?php
- } //end foreach
- }
- function room_facility_meta_save( $post_id )
- {
- // Verify this came from the our screen and with proper authorization,
- // because save_post can be triggered at other times
- //if ( !wp_verify_nonce( $_POST['room_facility_nonce'], plugin_basename(__FILE__) )) { <-- THIS DIDN'T WORK
- if(! isset( $_POST['room_facility_nonce'] )){
- return $post_id;
- }
- // Verify if this is an auto save routine. If it is our form has not been submitted, so we dont want
- // to do anything
- if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
- return $post_id;
- //wp_set_object_terms( 742, array(211), 'room_facility');
- $taxonomy = "room_facility";
- $term_facilities = get_terms( $taxonomy, array('hide_empty' => false));
- foreach ($term_facilities as $tax => $term)
- {
- $room_facility_id = $term->term_id;
- /* the meta_key for postmeta, note there is the room_facility included,
- * since there should be a relationship between post_id (in this case from taxonomy "room_type")
- * and "room_facility"
- */
- $meta_key = "_room_facility_key_".$room_facility_id;
- //update postmeta room_facility_key_<ID>
- if( isset( $_POST[ "text_".$room_facility_id] ) ) {
- update_post_meta( $post_id, $meta_key, sanitize_text_field( $_POST[ "text_".$room_facility_id] ) );
- }
- //set wp_term_relationships for room_type and room_facility
- if( isset( $_POST[ 'checkbox_'.$room_facility_id ] ) )
- {
- //wp_set_post_terms( $post_id, $term, $taxonomy); <-- WRONG function
- wp_set_object_terms( $post_id, array($term->term_id) , $taxonomy);
- } /*else {
- wp_set_post_terms( $post_id, $term, $taxonomy);
- }*/
- }
- }
- add_action( 'save_post', 'room_facility_meta_save' );
- echo '<script>alert("test")</script>';
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement