Попытался написать формирование метаполей сразу для двух типов записей с общей функцией инициализации и сохранения.
Вроде сработало, только вот пробелы в метаполях и вообще все, напсианное кирилличными сиволами ввырезает. Не подскажете, в чем тут может быть подвох?
Да и вообще, буду благодарен, если кто взгянет на адекватность всего, что я накрутил
add_action( 'load-post.php', 'mixa_post_meta_boxes_setup' );
add_action( 'load-post-new.php', 'mixa_post_meta_boxes_setup' );
function mixa_post_meta_boxes_setup() {
// fist metabox
add_action( 'add_meta_boxes', 'mixa_add_post_meta_boxes' );
// second metabox
add_action( 'add_meta_boxes', 'mixa2_add_post_meta_boxes' );
add_action( 'save_post', 'mixa_save_post_class_meta', 10, 2 );
}
/// !!!!
/* Create one or more meta boxes to be displayed on the post editor screen. */
function mixa_add_post_meta_boxes() {
add_meta_box(
'box1', // Unique ID
esc_html__( 'Бокс 1', 'example' ), // Title
'box1_meta_box', // Callback function
'nomer', // Admin page (or post type)
'normal', // normal, advanced, and side.
'default' // Priority
);
}
function mixa2_add_post_meta_boxes() {
add_meta_box(
'box2', // Unique ID
esc_html__( 'Бокс 2', 'example' ), // Title
'box2_meta_box', // Callback function
'post', // Admin page (or post type)
'normal', // normal, advanced, and side.
'default' // Priority
);
}
////
function box1_meta_box( $object, $box ) { ?>
<?php wp_nonce_field( basename( __FILE__ ), 'mixa_post_class_nonce' ); ?>
<p>
<label for="mixa-post-class"><?php _e( "Опис поля 1", 'example' ); ?></label>
<br />
<input class="widefat" type="text" name="mixa-post-class" id="mixa-post-class" value="<?php echo esc_attr( get_post_meta( $object->ID, 'mixa_post_class', true ) ); ?>" size="30" />
</p>
<?php }
function box2_meta_box( $object, $box ) { ?>
<?php wp_nonce_field( basename( __FILE__ ), 'mixa_post_class_nonce' ); ?>
<p>
<label for="mixa-post-class"><?php _e( "Опис поля 2", 'example' ); ?></label>
<br />
<input class="widefat" type="text" name="mixa-post-class" id="mixa-post-class" value="<?php echo esc_attr( get_post_meta( $object->ID, 'mixa_post_class', true ) ); ?>" size="30" />
</p>
<?php }
// COMMON FUNCTION
/* Save the meta box's post metadata. */
function mixa_save_post_class_meta( $post_id, $post ) {
/* Verify the nonce before proceeding. */
if ( !isset( $_POST['mixa_post_class_nonce'] ) || !wp_verify_nonce( $_POST['mixa_post_class_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['mixa-post-class'] ) ? sanitize_html_class( $_POST['mixa-post-class'] ) : '' );
/* Get the meta key. */
$meta_key = 'mixa_post_class';
/* 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 );
}