1

I've made a meta box field which works nice, but now i need to add one more. For the past few hours I've tried to mix every piece of it, to duplicate it, take some parts, google, search, and no luck. Second meta box appears but it wont save. So this is my first meta box. Can anyone help with making additional one. What needs to be added from first one to second/or removed. Some parts do not show well here so cleaner version http://pastebin.com/hYiYj1ZH

 /* Fire our meta box setup function on the post editor screen. */ add_action( 'load-post.php', 'sw_post_meta_boxes_setup' ); add_action( 'load-post-new.php', 'sw_post_meta_boxes_setup' ); /* Meta box setup function. */ function sw_post_meta_boxes_setup() { /* Add meta boxes on the 'add_meta_boxes' hook. */ add_action( 'add_meta_boxes', 'sw_add_post_meta_boxes' ); /* Save post meta on the 'save_post' hook. */ add_action( 'save_post', 'sw_save_video_box_meta', 10, 2 ); } /* Create one or more meta boxes to be displayed on the post editor screen. */ function sw_add_post_meta_boxes() { add_meta_box( 'sw_video_player', // Unique ID esc_html__( 'Video Box', 'video-text' ), // Title 'sw_video_box_meta_box', // Callback function 'job_listing',// Admin page (custom post type) 'normal', // Context 'high' // Priority ); add_meta_box( 'sw_face_player', // Unique ID esc_html__( 'Facebook Event Box', 'video-text' ), // Title 'sw_face_box_meta_box', // Callback function 'job_listing',// Admin page (custom post type) 'normal', // Context 'high' // Priority ); } /* Display the post meta box. */ if ( !function_exists( 'sw_video_box_meta_box' ) ) { function sw_video_box_meta_box( $object, $box ) { wp_nonce_field( basename( __FILE__ ), 'sw_video_box_nonce' ); _e( "Place Video/Playlist Embed Code Here.", 'sw-text' ); 
echo esc_html__( get_post_meta( $object->ID, 'sw_video_box', true ) ); } } /* Save the meta box's post metadata. */ if ( !function_exists( 'sw_save_video_box_meta' ) ) { function sw_save_video_box_meta( $post_id, $post ) { /* Verify the nonce before proceeding. */ if ( !isset( $_POST['sw_video_box_nonce'] ) || !wp_verify_nonce( $_POST['sw_video_box_nonce'], basename( __FILE__ ) ) ) return $post_id; /* Get the post type object. */ $post_type = get_post_type_object( $post->post_type ); /* Check if the current user has permission to edit the post. */ if ( !current_user_can( $post_type->cap->edit_post, $post_id ) ) return $post_id; /* Get the posted data and sanitize it for use as an HTML class. */ $new_meta_value = ( isset( $_POST['sw_video_player'] ) ? balanceTags( $_POST['sw_video_player'] ) : '' ); /* Get the meta key. */ $meta_key = 'sw_video_box'; /* Get the meta value of the custom field key. */ $meta_value = get_post_meta( $post_id, $meta_key, true ); /* If a new meta value was added and there was no previous value, add it. */ if ( $new_meta_value && '' == $meta_value ) add_post_meta( $post_id, $meta_key, $new_meta_value, true ); /* If the new meta value does not match the old value, update it. */ elseif ( $new_meta_value && $new_meta_value != $meta_value ) update_post_meta( $post_id, $meta_key, $new_meta_value ); /* If there is no new meta value but an old value exists, delete it. */ elseif ( '' == $new_meta_value && $meta_value ) delete_post_meta( $post_id, $meta_key, $meta_value ); } }
2
  • where is the function to save 'sw_face_box_meta_box' Commented May 4, 2016 at 10:52
  • Hey @HowardE in this snippet i dont have it. I wanted to make show one clean working one, and get a way to duplicate it. Why i didnt show that. Because I've tried with more than 10 ways, not sure which one to provide, that's why i opted not to..Basically i tried to copy and edit top part, bottom part, both..etc..ma no luck, doesnt save. Can you tell me only which lines i should copy and edit? Commented May 4, 2016 at 18:15

1 Answer 1

2

You didn't add the save meta action for the facebook meta box.

<?php /* Fire our meta box setup function on the post editor screen. */ add_action( 'load-post.php', 'sw_post_meta_boxes_setup' ); add_action( 'load-post-new.php', 'sw_post_meta_boxes_setup' ); /* Meta box setup function. */ function sw_post_meta_boxes_setup() { /* Add meta boxes on the 'add_meta_boxes' hook. */ add_action( 'add_meta_boxes', 'sw_add_post_meta_boxes' ); /* Save post meta on the 'save_post' hook. */ add_action( 'save_post', 'sw_save_video_box_meta', 10, 2); add_action( 'save_post', 'sw_save_face_box_meta', 10, 2); } /* Create one or more meta boxes to be displayed on the post editor screen. */ function sw_add_post_meta_boxes() { add_meta_box( 'sw_video_player', // Unique ID esc_html__( 'Video Box', 'video-text' ), // Title 'sw_video_box_meta_box', // Callback function 'job_listing',// Admin page (custom post type) 'normal', // Context 'high' // Priority ); add_meta_box( 'sw_face_player', // Unique ID esc_html__( 'Facebook Event Box', 'video-text' ), // Title 'sw_face_box_meta_box', // Callback function 'job_listing',// Admin page (custom post type) 'normal', // Context 'high' // Priority ); } /* Display the post meta box. */ if ( !function_exists( 'sw_video_box_meta_box' ) ) { function sw_video_box_meta_box( $object, $box ) { ?> <?php wp_nonce_field( basename( __FILE__ ), 'sw_video_box_nonce' ); ?> <p> <label for="sw_video_player"><?php _e( "Place Video/Playlist Embed Code Here.", 'sw-text' ); ?></label> <br /> <textarea class="widefat" name="sw_video_player" id="sw_video_player" cols="50" rows="5"><?php echo esc_html__( get_post_meta( $object->ID, 'sw_video_box', true ) ); ?></textarea> </p> <?php } // Facebook Meta Box if ( !function_exists( 'sw_face_box_meta_box' ) ) { function sw_face_box_meta_box( $object, $box ) { ?> <?php wp_nonce_field( basename( __FILE__ ), 'sw_face_box_nonce' ); ?> <p> <label for="sw_face_player"><?php _e( "Place FB Video/Playlist Embed Code Here.", 'sw-text' ); ?></label> <br /> <textarea class="widefat" name="sw_face_player" id="sw_face_player" cols="50" rows="5"><?php echo esc_html__( get_post_meta( $object->ID, 'sw_face_box', true ) ); ?></textarea> </p> <?php } } } /* Save the meta box's post metadata. */ if ( !function_exists( 'sw_save_video_box_meta' ) ) { function sw_save_video_box_meta( $post_id, $post ) { /* Verify the nonce before proceeding. */ if ( !isset( $_POST['sw_video_box_nonce'] ) || !wp_verify_nonce( $_POST['sw_video_box_nonce'], basename( __FILE__ ) ) ) return $post_id; /* Get the post type object. */ $post_type = get_post_type_object( $post->post_type ); /* Check if the current user has permission to edit the post. */ if ( !current_user_can( $post_type->cap->edit_post, $post_id ) ) return $post_id; /* Get the posted data and sanitize it for use as an HTML class. */ $new_meta_value = ( isset( $_POST['sw_video_player'] ) ? balanceTags( $_POST['sw_video_player'] ) : '' ); /* Get the meta key. */ $meta_key = 'sw_video_box'; /* Get the meta value of the custom field key. */ $meta_value = get_post_meta( $post_id, $meta_key, true ); /* If a new meta value was added and there was no previous value, add it. */ if ( $new_meta_value && '' == $meta_value ) add_post_meta( $post_id, $meta_key, $new_meta_value, true ); /* If the new meta value does not match the old value, update it. */ elseif ( $new_meta_value && $new_meta_value != $meta_value ) update_post_meta( $post_id, $meta_key, $new_meta_value ); /* If there is no new meta value but an old value exists, delete it. */ elseif ( '' == $new_meta_value && $meta_value ) delete_post_meta( $post_id, $meta_key, $meta_value ); } } /* Save the meta box's post metadata. Facebook Box */ if ( !function_exists( 'sw_save_face_box_meta' ) ) { function sw_save_face_box_meta( $post_id, $post ) { /* Verify the nonce before proceeding. */ if ( !isset( $_POST['sw_face_box_nonce'] ) || !wp_verify_nonce( $_POST['sw_face_box_nonce'], basename( __FILE__ ) ) ) return $post_id; /* Get the post type object. */ $post_type = get_post_type_object( $post->post_type ); /* Check if the current user has permission to edit the post. */ if ( !current_user_can( $post_type->cap->edit_post, $post_id ) ) return $post_id; /* Get the posted data and sanitize it for use as an HTML class. */ $new_meta_value = ( isset( $_POST['sw_face_player'] ) ? balanceTags( $_POST['sw_face_player'] ) : '' ); /* Get the meta key. */ $meta_key = 'sw_face_box'; /* Get the meta value of the custom field key. */ $meta_value = get_post_meta( $post_id, $meta_key, true ); /* If a new meta value was added and there was no previous value, add it. */ if ( $new_meta_value && '' == $meta_value ) add_post_meta( $post_id, $meta_key, $new_meta_value, true ); /* If the new meta value does not match the old value, update it. */ elseif ( $new_meta_value && $new_meta_value != $meta_value ) update_post_meta( $post_id, $meta_key, $new_meta_value ); /* If there is no new meta value but an old value exists, delete it. */ elseif ( '' == $new_meta_value && $meta_value ) delete_post_meta( $post_id, $meta_key, $meta_value ); } } 
1
  • Thank you a lot. Now i see that i did go way the correct way, but i missed just one line, that's why it wasnt working. It was this one add_action( 'save_post', 'sw_save_face_box_meta', 10, 2); Once again, thank you! Commented May 5, 2016 at 3:23

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.