Advertisement
amansdpr

Custom Meta Box Wordpress

Oct 5th, 2016
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.22 KB | None | 0 0
  1. // add custom meta box "Product Description" for post type "Product"
  2. add_action( 'add_meta_boxes', 'aim_product_description' );
  3. function aim_product_description() {
  4.     add_meta_box(
  5.         'product_des',
  6.         'Product Description',
  7.         'aim_p_des_output_func',
  8.         'product',
  9.         'normal'
  10.     );
  11. }
  12.  
  13. function aim_p_des_output_func( $post ){
  14.     wp_nonce_field( basename( __FILE__ ), 'aim_p_des_nonce' );
  15.     $apd_meta_stored = get_post_meta( $post->ID );
  16. ?>
  17.     <p>
  18.         <label for="file_size" style="width: 60px;display:inline-block;">File Size</label>
  19.         <input type="text" id="file_size" class="regular-text" name="_filesize"
  20.         value="<?php if ( ! empty( $apd_meta_stored['_filesize'] ) ) {
  21.                 echo esc_attr ( $apd_meta_stored['_filesize'][0] );
  22.              }?>" >
  23.     </p>
  24.     <p>
  25.         <label for="file_type" style="width: 60px;display:inline-block;">File Type</label>
  26.         <input type="text" id="file_type" class="regular-text" name="_filetype"
  27.         value="<?php if ( ! empty( $apd_meta_stored['_filetype'] ) ) {
  28.                 echo esc_attr ( $apd_meta_stored['_filetype'][0] );
  29.              }?>" >
  30.     </p>
  31.     <p>
  32.         <label for="file_url" style="width: 60px;display:inline-block;">File Url</label>
  33.         <input type="text" id="file_url" class="regular-text" name="_fileurl"
  34.         value="<?php if ( ! empty( $apd_meta_stored['_fileurl'] ) ) {
  35.                 echo esc_attr ( $apd_meta_stored['_fileurl'][0] );
  36.              }?>" >
  37.     </p>
  38. <?php
  39. }
  40.  
  41.  
  42. function apd_meta_save ( $post_id ) {
  43.     // Checks save status
  44.     $is_autosave = wp_is_post_autosave( $post_id );
  45.     $is_revision = wp_is_post_revision( $post_id );
  46.     $is_valid_nonce = ( isset( $_POST['aim_p_des_nonce'] ) && wp_verify_nonce( $_POST['aim_p_des_nonce'], basename( __FILE__ ) ) ) ? 'true' : 'false';
  47.  
  48.     // Exits script depending on save status
  49.     if ( $is_autosave || $is_revision || !$is_valid_nonce ) {
  50.         return;
  51.     }
  52.  
  53.     if ( isset( $_POST['_filesize'] ) ) {
  54.         update_post_meta( $post_id, '_filesize', sanitize_text_field( $_POST['_filesize'] ) );
  55.     }
  56.     if ( isset( $_POST['_filetype'] ) ) {
  57.         update_post_meta( $post_id, '_filetype', sanitize_text_field( $_POST['_filetype'] ) );
  58.     }
  59.     if ( isset( $_POST['_fileurl'] ) ) {
  60.         update_post_meta( $post_id, '_fileurl', sanitize_text_field( $_POST['_fileurl'] ) );
  61.     }
  62. }
  63. add_action( 'save_post', 'apd_meta_save' );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement