RuslanK

custum fieds new

Feb 12th, 2020
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.46 KB | None | 0 0
  1. ## Добавляем блоки в основную колонку на страницах постов и пост. страниц
  2. add_action('add_meta_boxes', 'myplugin_add_custom_box');
  3. function myplugin_add_custom_box(){
  4.     $screens = array( 'Video' );
  5.     add_meta_box( 'myplugin_sectionid', 'Дополнительная информация', 'myplugin_meta_box_callback', $screens );
  6. }
  7.  
  8. // HTML код блока
  9. function myplugin_meta_box_callback( $post, $meta ){
  10.     $screens = $meta['args'];
  11.  
  12.     // Используем nonce для верификации
  13.     wp_nonce_field( plugin_basename(__FILE__), 'myplugin_noncename' );
  14.  
  15.     // значение поля
  16.     $author = get_post_meta( $post->ID, 'meta_author', 1);
  17.   $actors = get_post_meta( $post->ID, 'meta_actors',1);
  18.   //var_dump($author);echo "<br>";
  19.  
  20.     // Поля формы для введения данных
  21.     echo '<label for="new_field">' . __("Автор", 'textdomain' ) . '</label> ';
  22.     echo '<input type="text" id="new_field" name="new_field" author="'. $author .'" size="25" />';
  23.  
  24.   echo '<label for="new_field">' . __("В ролях", 'textdomain' ) . '</label> ';
  25.   echo '<input type="text" id="new_field2" name="new_field2" author="'. $actors.'" size="25" />';
  26. }
  27.  
  28.  
  29. ## Сохраняем данные, когда пост сохраняется
  30. add_action( 'save_post', 'myplugin_save_postdata' );
  31. function myplugin_save_postdata( $post_id ) {
  32.     // Убедимся что поле установлено.
  33.     if ( ! isset( $_POST['new_field'] ) )
  34.         return;
  35.  
  36.     // проверяем nonce нашей страницы, потому что save_post может быть вызван с другого места.
  37.     if ( ! wp_verify_nonce( $_POST['myplugin_noncename'], plugin_basename(__FILE__) ) )
  38.         return;
  39.  
  40.     // если это автосохранение ничего не делаем
  41.     if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
  42.         return;
  43.  
  44.     // проверяем права юзера
  45.     if( ! current_user_can( 'edit_post', $post_id ) )
  46.         return;
  47.  
  48.     // Все ОК. Теперь, нужно найти и сохранить данные
  49.     // Очищаем значение поля input.
  50.     $author_data = sanitize_text_field( $_POST['new_field'] );
  51.   $actors_data = sanitize_text_field( $_POST['new_field2'] );
  52.  
  53.     // Обновляем данные в базе данных.
  54.     update_post_meta( $post_id, 'meta_author', $author_data );
  55.   update_post_meta( $post_id, 'meta_actors', $actors_data );
  56. }
Advertisement
Add Comment
Please, Sign In to add comment