Advertisement
Guest User

Untitled

a guest
Oct 19th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.85 KB | None | 0 0
  1. function p52_register_quote_meta_boxes( $meta_boxes ) {
  2.  
  3.     $prefix = 'p52_quote_';
  4.  
  5.     // Get all customer custom post type posts
  6.     $customers = get_posts( array(
  7.         'posts_per_page' => -1,
  8.         'post_type' => 'customer'
  9.     ) );
  10.  
  11.     // Define main meta box
  12.     $meta_box = array(
  13.         'id' => $prefix . 'customer',
  14.         'title' => 'Customer Info',
  15.         // Should define post types to display in array.
  16.         // If you want this displays in Post, then assign to post here
  17.         'pages' => array( 'post' ),
  18.         'context' => 'normal',
  19.         'priority' => 'low',
  20.  
  21.         'fields' => array(
  22.             array(
  23.                 'name' => 'Name',
  24.                 'id' => $prefix . 'name',
  25.                 'type' => 'post',
  26.                 'post_type' => 'customer',
  27.                 'placeholder' => 'Select a Customer'
  28.             ),
  29.             // Real field
  30.             array(
  31.                 'name'      => 'Phone',
  32.                 'id'        => $prefix . 'phone',
  33.                 'type'      => 'hidden'
  34.             )
  35.         )
  36.     );
  37.  
  38.     // Append phone or another field of customer to meta box above.
  39.     foreach ( $customers as $customer )
  40.     {
  41.         // Show this phone field if match name.
  42.         $meta_box['fields'][] = array(
  43.             'name'      => 'Phone',
  44.             'id'        => $prefix . 'phone_' . $customer->ID,
  45.             'type'      => 'text',
  46.             'std'       => get_post_meta( $customer->ID, 'p52_quote_phone', true ),
  47.             'visible'   => array( $prefix . 'name', $customer->ID )
  48.         );
  49.     }
  50.  
  51.     $meta_boxes[] = $meta_box;
  52.  
  53.     return $meta_boxes;
  54. }
  55.  
  56. add_filter( 'rwmb_meta_boxes', 'p52_register_quote_meta_boxes' );
  57.  
  58. // Save data to phone field
  59. add_action('rwmb_before_save_post', function($post_id)
  60. {
  61.     // Get person ID to save from "Select a Customer" field
  62.     $person_to_save = intval( $_POST['p52_quote_name'] );
  63.  
  64.     // Save related field to phone field
  65.     $_POST['p52_quote_phone'] = $_POST['p52_quote_phone_' . $person_to_save];
  66.  
  67.     // Unset all hidden fields
  68.     foreach ( $_POST as $key => $value )
  69.     {
  70.         if ( strpos( $key, 'p52_quote_phone_' ) )
  71.             unset( $_POST[$key] );
  72.     }
  73. } );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement