Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## Добавляем блоки в основную колонку на страницах постов и пост. страниц
- add_action('add_meta_boxes', 'myplugin_add_custom_box');
- function myplugin_add_custom_box(){
- $screens = array( 'Video' );
- add_meta_box( 'myplugin_sectionid', 'Дополнительная информация', 'myplugin_meta_box_callback', $screens );
- }
- // HTML код блока
- function myplugin_meta_box_callback( $post, $meta ){
- $screens = $meta['args'];
- // Используем nonce для верификации
- wp_nonce_field( plugin_basename(__FILE__), 'myplugin_noncename' );
- // значение поля
- $author = get_post_meta( $post->ID, 'meta_author', 1);
- $actors = get_post_meta( $post->ID, 'meta_actors',1);
- //var_dump($author);echo "<br>";
- // Поля формы для введения данных
- echo '<label for="new_field">' . __("Автор", 'textdomain' ) . '</label> ';
- echo '<input type="text" id="new_field" name="new_field" author="'. $author .'" size="25" />';
- echo '<label for="new_field">' . __("В ролях", 'textdomain' ) . '</label> ';
- echo '<input type="text" id="new_field2" name="new_field2" author="'. $actors.'" size="25" />';
- }
- ## Сохраняем данные, когда пост сохраняется
- add_action( 'save_post', 'myplugin_save_postdata' );
- function myplugin_save_postdata( $post_id ) {
- // Убедимся что поле установлено.
- if ( ! isset( $_POST['new_field'] ) )
- return;
- // проверяем nonce нашей страницы, потому что save_post может быть вызван с другого места.
- if ( ! wp_verify_nonce( $_POST['myplugin_noncename'], plugin_basename(__FILE__) ) )
- return;
- // если это автосохранение ничего не делаем
- if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
- return;
- // проверяем права юзера
- if( ! current_user_can( 'edit_post', $post_id ) )
- return;
- // Все ОК. Теперь, нужно найти и сохранить данные
- // Очищаем значение поля input.
- $author_data = sanitize_text_field( $_POST['new_field'] );
- $actors_data = sanitize_text_field( $_POST['new_field2'] );
- // Обновляем данные в базе данных.
- update_post_meta( $post_id, 'meta_author', $author_data );
- update_post_meta( $post_id, 'meta_actors', $actors_data );
- }
Advertisement
Add Comment
Please, Sign In to add comment