Advertisement
firoze

Creating meta box according to post format

Aug 4th, 2018
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.90 KB | None | 0 0
  1. // Creating meta box according to post format      
  2.  
  3.  
  4. abstract class WPOrg_Meta_Box
  5.         {
  6.             public static function add()
  7.             {
  8.                 $screens = ['post', 'wporg_cpt'];
  9.                 foreach ($screens as $screen) {
  10.                         if ( has_post_format('video') ) {
  11.                                     add_meta_box(
  12.                                     'wporg_box_id',          // Unique ID
  13.                                     'Meta box For Video',
  14.                                     [self::class, 'html'],   // Content callback, must be of type callable
  15.                                     $screen                  // Post type
  16.                                 );
  17.                          }
  18.                         if ( has_post_format('Audio') ) {
  19.                                     add_meta_box(
  20.                                     'wporg_box_id',          // Unique ID
  21.                                     'Meta box For Audio',
  22.                                     [self::class, 'html'],   // Content callback, must be of type callable
  23.                                     $screen                  // Post type
  24.                                 );
  25.                          }
  26.  
  27.                 }
  28.             }
  29.          
  30.             public static function save($post_id)
  31.             {
  32.                 if (array_key_exists('wporg_field', $_POST)) {
  33.                     update_post_meta(
  34.                         $post_id,
  35.                         '_wporg_meta_key',
  36.                         $_POST['wporg_field']
  37.                     );
  38.                 }
  39.             }
  40.          
  41.  
  42.  
  43.  
  44.             public static function html($post)
  45.             {
  46.                 $value = get_post_meta($post->ID, '_wporg_meta_key', true);
  47.  
  48.                 if ( has_post_format('video') ) {
  49.                 ?>
  50.                  <label for="wporg_field">Meta Field For Video</label>
  51.                   <input class="text-regular" type="text" >
  52.                 <?php
  53.                 }
  54.                 if ( has_post_format('audio') ) {
  55.                 ?>
  56.                  <label for="wporg_field">Meta Field For Audio</label>
  57.                  <input  class="text-regular" type="text" >
  58.                 <?php
  59.                 }
  60.  
  61.             }
  62.         }
  63.          
  64.         add_action('add_meta_boxes', ['WPOrg_Meta_Box', 'add']);
  65.         add_action('save_post', ['WPOrg_Meta_Box', 'save']);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement