dwp-metaboxes.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. /***********************************************************
  3. ############ Media URL (dwp23_media_url_meta) ##############
  4. ************************************************************/
  5. add_action('init', 'dwp23_guten_register_media_meta');
  6. function dwp23_guten_register_media_meta() {
  7. register_meta('post', 'dwp23_media_url_meta', array(
  8. 'show_in_rest' => true,
  9. 'type' => 'string',
  10. 'single' => true,
  11. 'sanitize_callback' => 'sanitize_text_field',
  12. 'auth_callback' => function() {
  13. return current_user_can('edit_posts');
  14. }
  15. ));
  16. }
  17. add_action( 'add_meta_boxes', 'dwp23_guten_add_media_meta_box' );
  18. function dwp23_guten_add_media_meta_box() {
  19. add_meta_box(
  20. 'dwp23_guten_post_options_metabox',
  21. 'Post Options',
  22. 'dwp23_guten_post_options_metabox_callback',
  23. 'post',
  24. 'normal',
  25. 'default',
  26. array('__back_compat_meta_box' => true)
  27. );
  28. }
  29. add_action( 'save_post', 'dwp23_guten_save_post_metabox', 10, 2 );
  30. function dwp23_guten_post_options_metabox_callback($post) {
  31. $field_value = get_post_meta($post->ID, 'dwp23_media_url_meta', true);
  32. wp_nonce_field( 'dwp23_guten_update_post_metabox', 'dwp23_guten_update_post_nonce' );
  33. ?>
  34. <p>
  35. <label for="dwp23_guten_media_metafield"><?php esc_html_e( 'Media URL', 'textdomain' ); ?></label>
  36. <br />
  37. <input class="widefat" type="text" name="dwp23_guten_media_metafield" id="dwp23_guten_media_metafield" value="<?php echo esc_attr( $field_value ); ?>" />
  38. </p>
  39. <?php
  40. }
  41. function dwp23_guten_save_post_metabox($post_id, $post) {
  42. $edit_cap = get_post_type_object( $post->post_type )->cap->edit_post;
  43. if( !current_user_can( $edit_cap, $post_id )) {
  44. return;
  45. }
  46. if( !isset( $_POST['dwp23_guten_update_post_nonce']) || !wp_verify_nonce( $_POST['dwp23_guten_update_post_nonce'], 'dwp23_guten_update_post_metabox' )) {
  47. return;
  48. }
  49. if(array_key_exists('dwp23_guten_media_metafield', $_POST)) {
  50. update_post_meta(
  51. $post_id,
  52. 'dwp23_media_url_meta',
  53. sanitize_text_field($_POST['dwp23_guten_media_metafield'])
  54. );
  55. }
  56. }
  57. /***********************************************************
  58. ########## Media Poster URL (dwp23_poster_url_meta) ##########
  59. ************************************************************/
  60. add_action('init', 'dwp23_guten_register_poster_url_meta');
  61. function dwp23_guten_register_poster_url_meta() {
  62. register_meta('post', 'dwp23_poster_url_meta', array(
  63. 'show_in_rest' => true,
  64. 'type' => 'string',
  65. 'single' => true,
  66. 'sanitize_callback' => 'sanitize_text_field',
  67. 'auth_callback' => function() {
  68. return current_user_can('edit_posts');
  69. }
  70. ));
  71. }
  72. add_action( 'add_meta_boxes', 'dwp23_guten_add_poster_url_meta_box' );
  73. function dwp23_guten_add_poster_url_meta_box() {
  74. add_meta_box(
  75. 'dwp23_guten_post_options_metabox',
  76. 'Post Options',
  77. 'dwp23_guten_post_options_metabox_poster_url',
  78. 'post',
  79. 'normal',
  80. 'default',
  81. array('__back_compat_meta_box' => true)
  82. );
  83. }
  84. function dwp23_guten_post_options_metabox_poster_url($post) {
  85. $field_value = get_post_meta($post->ID, 'dwp23_image_meta_url', true);
  86. wp_nonce_field( 'dwp23_guten_update_post_metabox_poster_url', 'dwp23_guten_update_post_nonce' );
  87. ?>
  88. <p>
  89. <label for="dwp23_guten_poster_url_metafield"><?php esc_html_e( 'Poster Image URL', 'textdomain' ); ?></label>
  90. <br />
  91. <input class="widefat" type="text" name="dwp23_guten_poster_url_metafield" id="dwp23_guten_poster_url_metafield" value="<?php echo esc_attr( $field_value ); ?>" />
  92. </p>
  93. <?php
  94. }
  95. add_action( 'save_post', 'dwp23_guten_save_post_metabox_poster_image_url', 10, 2 );
  96. function dwp23_guten_save_post_metabox_poster_image_url($post_id, $post) {
  97. $edit_cap = get_post_type_object( $post->post_type )->cap->edit_post;
  98. if( !current_user_can( $edit_cap, $post_id )) {
  99. return;
  100. }
  101. 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' )) {
  102. return;
  103. }
  104. if(array_key_exists('dwp23_guten_media_metafield', $_POST)) {
  105. update_post_meta(
  106. $post_id,
  107. 'dwp23_image_meta_url',
  108. sanitize_text_field($_POST['dwp23_guten_featured_image_url_metafield'])
  109. );
  110. }
  111. }