123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- <?php
- /***********************************************************
- ############ Media URL (dwp23_media_url_meta) ##############
- ************************************************************/
- add_action('init', 'dwp23_guten_register_media_meta');
- function dwp23_guten_register_media_meta() {
- register_meta('post', 'dwp23_media_url_meta', array(
- 'show_in_rest' => true,
- 'type' => 'string',
- 'single' => true,
- 'sanitize_callback' => 'sanitize_text_field',
- 'auth_callback' => function() {
- return current_user_can('edit_posts');
- }
- ));
- }
- add_action( 'add_meta_boxes', 'dwp23_guten_add_media_meta_box' );
- function dwp23_guten_add_media_meta_box() {
- add_meta_box(
- 'dwp23_guten_post_options_metabox',
- 'Post Options',
- 'dwp23_guten_post_options_metabox_callback',
- 'post',
- 'normal',
- 'default',
- array('__back_compat_meta_box' => true)
- );
- }
- add_action( 'save_post', 'dwp23_guten_save_post_metabox', 10, 2 );
- function dwp23_guten_post_options_metabox_callback($post) {
- $field_value = get_post_meta($post->ID, 'dwp23_media_url_meta', true);
- wp_nonce_field( 'dwp23_guten_update_post_metabox', 'dwp23_guten_update_post_nonce' );
- ?>
- <p>
- <label for="dwp23_guten_media_metafield"><?php esc_html_e( 'Media URL', 'textdomain' ); ?></label>
- <br />
- <input class="widefat" type="text" name="dwp23_guten_media_metafield" id="dwp23_guten_media_metafield" value="<?php echo esc_attr( $field_value ); ?>" />
- </p>
- <?php
- }
- function dwp23_guten_save_post_metabox($post_id, $post) {
- $edit_cap = get_post_type_object( $post->post_type )->cap->edit_post;
- if( !current_user_can( $edit_cap, $post_id )) {
- return;
- }
- if( !isset( $_POST['dwp23_guten_update_post_nonce']) || !wp_verify_nonce( $_POST['dwp23_guten_update_post_nonce'], 'dwp23_guten_update_post_metabox' )) {
- return;
- }
- if(array_key_exists('dwp23_guten_media_metafield', $_POST)) {
- update_post_meta(
- $post_id,
- 'dwp23_media_url_meta',
- sanitize_text_field($_POST['dwp23_guten_media_metafield'])
- );
- }
- }
- /***********************************************************
- ########## Media Poster URL (dwp23_poster_url_meta) ##########
- ************************************************************/
- add_action('init', 'dwp23_guten_register_poster_url_meta');
- function dwp23_guten_register_poster_url_meta() {
- register_meta('post', 'dwp23_poster_url_meta', array(
- 'show_in_rest' => true,
- 'type' => 'string',
- 'single' => true,
- 'sanitize_callback' => 'sanitize_text_field',
- 'auth_callback' => function() {
- return current_user_can('edit_posts');
- }
- ));
- }
- add_action( 'add_meta_boxes', 'dwp23_guten_add_poster_url_meta_box' );
- function dwp23_guten_add_poster_url_meta_box() {
- add_meta_box(
- 'dwp23_guten_post_options_metabox',
- 'Post Options',
- 'dwp23_guten_post_options_metabox_poster_url',
- 'post',
- 'normal',
- 'default',
- array('__back_compat_meta_box' => true)
- );
- }
- function dwp23_guten_post_options_metabox_poster_url($post) {
- $field_value = get_post_meta($post->ID, 'dwp23_image_meta_url', true);
- wp_nonce_field( 'dwp23_guten_update_post_metabox_poster_url', 'dwp23_guten_update_post_nonce' );
- ?>
- <p>
- <label for="dwp23_guten_poster_url_metafield"><?php esc_html_e( 'Poster Image URL', 'textdomain' ); ?></label>
- <br />
- <input class="widefat" type="text" name="dwp23_guten_poster_url_metafield" id="dwp23_guten_poster_url_metafield" value="<?php echo esc_attr( $field_value ); ?>" />
- </p>
- <?php
- }
- add_action( 'save_post', 'dwp23_guten_save_post_metabox_poster_image_url', 10, 2 );
- function dwp23_guten_save_post_metabox_poster_image_url($post_id, $post) {
- $edit_cap = get_post_type_object( $post->post_type )->cap->edit_post;
- if( !current_user_can( $edit_cap, $post_id )) {
- return;
- }
- if( !isset( $_POST['dwp23_guten_update_post_nonce_featured_image_url']) || !wp_verify_nonce( $_POST['dwp23_guten_update_post_nonce_featured_image_url'], 'dwp23_guten_update_post_metabox_featured_image_url' )) {
- return;
- }
- if(array_key_exists('dwp23_guten_media_metafield', $_POST)) {
- update_post_meta(
- $post_id,
- 'dwp23_image_meta_url',
- sanitize_text_field($_POST['dwp23_guten_featured_image_url_metafield'])
- );
- }
- }
|