Advertisement
rexcoder

Custom Metabox

Mar 23rd, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.46 KB | None | 0 0
  1. function tp_custom_meta_boxes() {
  2.     add_meta_box('tp_cs_meta', 'TP Meta', 'tp_custom_post_meta', 'post', 'side', 'high');
  3. }
  4. add_action( 'add_meta_boxes', 'tp_custom_meta_boxes' );
  5.  
  6.  
  7. function tp_custom_post_meta( $post ) {
  8.     $photo_credit = get_post_meta( $post->ID, '__photo_credit', true );
  9.     $photo_credit = isset($photo_credit) ? $photo_credit : '';
  10.    
  11.     ?>
  12.         <p>
  13.             <div class="row-content">
  14.             <label for="featured-post">
  15.                 <input type="text" name="photo_credit" id="photo_credit" value="<?php echo esc_attr($photo_credit); ?>" class="widefat">           
  16.                 <?php _e( 'Featured', 'themeplate' )?>
  17.             </label>
  18.         </div>
  19.         </p>
  20.     <?php
  21. }
  22.  
  23.  
  24. // Save metadata
  25. function tp_save_post_meta( $post_id, $post ) {
  26.     //use this for the multiple post type
  27.     $edit_capability = get_post_type_object( $post->post_type );
  28.  
  29.     if( !current_user_can(), 'edit_post', $post_id ) {
  30.         return;
  31.     }
  32.     $is_autosave = wp_is_post_autosave( $post_id );
  33.     $is_revision = wp_is_post_revision( $post_id );
  34.  
  35.     if ( $is_autosave || $is_revision ) {
  36.         return;
  37.     }
  38.  
  39.     if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
  40.         return;
  41.     }
  42.  
  43.     if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
  44.         return;
  45.     }
  46.  
  47.     $photo_credit = !empty( $_POST['photo_credit'] ) ? sanitize_text_field($_POST['photo_credit']) : '';
  48.     if( array_key_exists('photo_credit', $_POST ) ) {
  49.         update_post_meta( $post_id, '__photo_credit', $photo_credit );
  50.     }
  51. }
  52. add_action( 'save_post', 'tp_save_post_meta', 10, 2 );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement