Как дублировать (несколько мета-полей)?
Я создал поле мета-поля, которое хорошо работает, но теперь мне нужно добавить еще одно. В течение последних нескольких часов я пытался смешать каждую его часть, скопировать ее, взять некоторые части, погуглить, поискать, но безуспешно. Появится второе мета-поле, но оно не сохранится. Итак, это мой первый мета-бокс. Может ли кто-нибудь помочь с созданием дополнительного. Что нужно добавить от первого ко второму/или удалить. Некоторые части не очень хорошо отображаются здесь, поэтому более чистая версия 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 ); } }
1
1 answers
Вы не добавили действие сохранить мета для мета-поля facebook.
<?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 );
}
}
2
Author: Howard E, 2016-05-04 20:13:30