Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function my_theme_init() {
- /**
- * Кастомный тип записи
- */
- register_post_type( 'person', array(
- 'label' => 'Сотрудник',
- 'description' => 'Список сотрудников',
- 'labels' => array(
- 'name' => 'Сотрудники',
- 'singular_name' => 'Сотрудник',
- 'menu_name' => 'Сотрудники',
- 'all_items' => 'Список сотрудников',
- 'add_new_item' => 'Новый сотрудник',
- 'add_new' => 'Добавить сотрудника',
- 'edit_item' => 'Редактировать данные',
- 'update_item' => 'Обновить данные',
- ),
- 'supports' => array( 'title', 'editor', 'thumbnail', 'page-attributes' ),
- 'hierarchical' => false,
- 'public' => false,
- 'show_ui' => true,
- 'show_in_menu' => true,
- 'show_in_nav_menus' => false,
- 'show_in_admin_bar' => false,
- 'menu_position' => 4,
- 'can_export' => false,
- 'has_archive' => false,
- 'exclude_from_search' => true,
- 'publicly_queryable' => false,
- ) );
- }
- add_action( 'init', 'my_theme_init' );
- /**
- * Добавляем мета бокс на типа записи 'person'
- */
- function person_post_add_meta_box() {
- add_meta_box(
- 'person_post_sectionid',
- 'Должность',
- 'person_post_meta_box_callback',
- 'person'
- );
- }
- add_action( 'add_meta_boxes', 'person_post_add_meta_box' );
- /**
- * Выводим мета бокс в админке
- *
- * @param WP_Post $post
- */
- function person_post_meta_box_callback( $post ) {
- wp_nonce_field( 'person_post_meta_box', 'person_post_meta_box_nonce' );
- $value = get_post_meta( $post->ID, 'person_post', true );
- echo '<label for="person_post_new_field">Должность</label> ';
- echo '<input type="text" id="person_post_new_field" name="person_post_new_field" value="' . esc_attr( $value ) . '">';
- }
- /**
- * Проверяем и сохраняем данные из мета бокса
- *
- * @param int $post_id
- */
- function person_post_save_meta_box_data( $post_id ) {
- if ( ! isset( $_POST['person_post_meta_box_nonce'] ) ) {
- return;
- }
- if ( ! wp_verify_nonce( $_POST['person_post_meta_box_nonce'], 'person_post_meta_box' ) ) {
- return;
- }
- if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
- return;
- }
- if ( isset( $_POST['post_type'] ) && 'page' == $_POST['post_type'] ) {
- if ( ! current_user_can( 'edit_page', $post_id ) ) {
- return;
- }
- } else {
- if ( ! current_user_can( 'edit_post', $post_id ) ) {
- return;
- }
- }
- if ( ! isset( $_POST['person_post_new_field'] ) ) {
- return;
- }
- $my_data = sanitize_text_field( $_POST['person_post_new_field'] );
- update_post_meta( $post_id, 'person_post', $my_data );
- }
- add_action( 'save_post', 'person_post_save_meta_box_data' );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement