Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /* A simple way to create metaboxes in WordPress */
- add_action( 'add_meta_boxes', 'video_meta_box' );
- function video_meta_box()
- {
- /*
- http://codex.wordpress.org/Function_Reference/add_meta_box
- add_meta_box( $id, $title, $callback, $post_type, $context, $priority, $callback_args );
- */
- add_meta_box( 'video_meta_box', 'Video', 'video_meta_box_setup', 'post', 'normal', 'high' );
- }
- function video_meta_box_setup()
- {
- global $post;
- /* http://codex.wordpress.org/Function_Reference/wp_nonce_field */
- wp_nonce_field( plugin_basename( __FILE__ ), 'video_meta_box_nonce_name' );
- /* we take the data stored separately here... */
- $video_id = get_post_meta( $post->ID, '_video_id', true);
- $video_image = get_post_meta( $post->ID, '_video_image', true);
- /*
- If you have stored all data in one array.....
- $video_data = get_post_meta( $post->ID, '_video_data', true);
- $video_id = $video_data['video_id'];
- $video_image = $video_data['video_image'];
- */
- ?>
- <table class="form-table">
- <tr>
- <td><label for="video_id">Informe o ID do vídeo</label></td>
- <td>
- <input class="regular-text" type="text" id="video_id" name="video[video_id]" value="<?php echo $video_id?>" />
- </td>
- </tr>
- <tr>
- <td><label for="projeto">Informe a URL da imagem do vídeo (thumbnail)</label></td>
- <td>
- <input class="regular-text" type="text" id="video_image" name="video[video_image]" value="<?php echo $video_image?>" />
- </td>
- </tr>
- </table>
- <?php }
- add_action( 'save_post', 'video_meta_box_save' );
- function video_meta_box_save( $post_id ) {
- // verify if this is an auto save routine.
- // If it is our form has not been submitted, so we dont want to do anything
- if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
- return;
- // verify this came from the our screen and with proper authorization,
- // because save_post can be triggered at other times
- if ( !wp_verify_nonce( $_POST['video_meta_box_nonce_name'], plugin_basename( __FILE__ ) ) )
- return;
- //check permissions
- if ( ! current_user_can( 'edit_post', $post_id ) ) return $post_id;
- $video = $_POST['video'];
- /* To store in separate meta fields */
- update_post_meta( $post_id, '_video_id', $video['video_id'] );
- update_post_meta( $post_id, '_video_image', $video['video_image'] );
- /* To store all data into one array
- update_post_meta( $post_id, '_video_data', $video );
- */
- return $post_id;
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement