Advertisement
lemb

WP custom page functions.php

May 29th, 2014
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.92 KB | None | 0 0
  1. function my_theme_init() {
  2.  
  3.     /**
  4.      * Кастомный тип записи
  5.      */
  6.     register_post_type( 'person', array(
  7.         'label'               => 'Сотрудник',
  8.         'description'         => 'Список сотрудников',
  9.         'labels'              => array(
  10.             'name'          => 'Сотрудники',
  11.             'singular_name' => 'Сотрудник',
  12.             'menu_name'     => 'Сотрудники',
  13.             'all_items'     => 'Список сотрудников',
  14.             'add_new_item'  => 'Новый сотрудник',
  15.             'add_new'       => 'Добавить сотрудника',
  16.             'edit_item'     => 'Редактировать данные',
  17.             'update_item'   => 'Обновить данные',
  18.         ),
  19.         'supports'            => array( 'title', 'editor', 'thumbnail', 'page-attributes' ),
  20.         'hierarchical'        => false,
  21.         'public'              => false,
  22.         'show_ui'             => true,
  23.         'show_in_menu'        => true,
  24.         'show_in_nav_menus'   => false,
  25.         'show_in_admin_bar'   => false,
  26.         'menu_position'       => 4,
  27.         'can_export'          => false,
  28.         'has_archive'         => false,
  29.         'exclude_from_search' => true,
  30.         'publicly_queryable'  => false,
  31.     ) );
  32. }
  33. add_action( 'init', 'my_theme_init' );
  34.  
  35.  
  36. /**
  37.  * Добавляем мета бокс на типа записи 'person'
  38.  */
  39. function person_post_add_meta_box() {
  40.     add_meta_box(
  41.         'person_post_sectionid',
  42.         'Должность',
  43.         'person_post_meta_box_callback',
  44.         'person'
  45.     );
  46. }
  47. add_action( 'add_meta_boxes', 'person_post_add_meta_box' );
  48.  
  49.  
  50. /**
  51.  * Выводим мета бокс в админке
  52.  *
  53.  * @param WP_Post $post
  54.  */
  55. function person_post_meta_box_callback( $post ) {
  56.  
  57.     wp_nonce_field( 'person_post_meta_box', 'person_post_meta_box_nonce' );
  58.  
  59.     $value = get_post_meta( $post->ID, 'person_post', true );
  60.  
  61.     echo '<label for="person_post_new_field">Должность</label> ';
  62.     echo '<input type="text" id="person_post_new_field" name="person_post_new_field" value="' . esc_attr( $value ) . '">';
  63. }
  64.  
  65. /**
  66.  * Проверяем и сохраняем данные из мета бокса
  67.  *
  68.  * @param int $post_id
  69.  */
  70. function person_post_save_meta_box_data( $post_id ) {
  71.     if ( ! isset( $_POST['person_post_meta_box_nonce'] ) ) {
  72.         return;
  73.     }
  74.  
  75.     if ( ! wp_verify_nonce( $_POST['person_post_meta_box_nonce'], 'person_post_meta_box' ) ) {
  76.         return;
  77.     }
  78.  
  79.     if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
  80.         return;
  81.     }
  82.  
  83.     if ( isset( $_POST['post_type'] ) && 'page' == $_POST['post_type'] ) {
  84.  
  85.         if ( ! current_user_can( 'edit_page', $post_id ) ) {
  86.             return;
  87.         }
  88.  
  89.     } else {
  90.  
  91.         if ( ! current_user_can( 'edit_post', $post_id ) ) {
  92.             return;
  93.         }
  94.     }
  95.  
  96.     if ( ! isset( $_POST['person_post_new_field'] ) ) {
  97.         return;
  98.     }
  99.  
  100.     $my_data = sanitize_text_field( $_POST['person_post_new_field'] );
  101.  
  102.     update_post_meta( $post_id, 'person_post', $my_data );
  103. }
  104. add_action( 'save_post', 'person_post_save_meta_box_data' );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement