Advertisement
Guest User

Untitled

a guest
Oct 19th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.28 KB | None | 0 0
  1. function p52_quote_post_type() {
  2.     register_post_type( 'p52-quote',
  3.         array(
  4.             'labels' => array(
  5.                 'name' => _x('Quote', 'post type general name'),
  6.                 'singular_name' => _x('Quote', 'post type singular name'),
  7.                 'add_new' => _x('Add New', 'quote'),
  8.                 'add_new_item' => __('Add New Quote'),
  9.                 'edit_item' => __('Edit Quote'),
  10.                 'new_item' => __('New Quote'),
  11.                 'all_items' => __('All Quotes'),
  12.                 'view_item' => __('View Quote'),
  13.                 'search_items' => __('Search Quotes'),
  14.                 'not_found' =>  __('Quote wasn\'t found'),
  15.                 'not_found_in_trash' => __('Quote wasn\'t found in Trash'),
  16.                 'parent_item_colon' => '',
  17.                 'menu_name' => 'P52 Quotes'            
  18.             ),
  19.             'public' => true,
  20.             'publicly_queryable' => true,
  21.             'show_in_menu' => true,
  22.             'query_var' => true,
  23.             'supports' => array( 'title' ),
  24.             'capability_type' => 'post',
  25.             'rewrite' => true,
  26.             'menu_position' => 5,
  27.             'hierarchical' => false,
  28.             'menu_icon' => 'dashicons-admin-links',
  29.         )
  30.     );
  31. }
  32. add_action( 'init', 'p52_quote_post_type' );
  33.  
  34. /***************************
  35. * create contact metaboxes
  36. ***************************/
  37. function p52_register_quote_contact_meta_boxes( $meta_boxes ) {
  38.     $prefix = 'p52_quote_';
  39.    
  40.     // Get all customer custom post type posts
  41.     $customers = get_posts( array(
  42.         'posts_per_page' => -1,
  43.         'post_type' => 'p52-contact'
  44.     ) );
  45.    
  46.     // start customer
  47.     $meta_boxes[] = array(
  48.         'id'       => $prefix . 'customer',
  49.         'title'    => 'Customer Info',
  50.         'pages'    => 'p52-quote',
  51.         'context'  => 'normal',
  52.         'priority' => 'low',
  53.        
  54.  
  55.         'fields' => array(            
  56.        
  57.             array(
  58.                 'name'  => 'Name',
  59.                 'id'    => $prefix . 'name',
  60.                 'type'  => 'post',
  61.                 'post_type' => 'p52-contact',
  62.                 'placeholder' => 'Select a Customer',
  63.                 'columns' => 6
  64.             ),
  65.        
  66.            
  67.             array(
  68.                 'name'  => 'Company',
  69.                 'id'    => $prefix . 'company',
  70.                 'type'  => 'text',
  71.                 'columns' => 6
  72.             ),
  73.            
  74.             array(
  75.                 'name'  => 'Email',
  76.                 'id'    => $prefix . 'email',
  77.                 'type'  => 'text',
  78.                 'columns' => 6
  79.             ),
  80.            
  81.             array(
  82.                 'name'  => 'Phone',
  83.                 'id'    => $prefix . 'phone',
  84.                 'type'  => 'hidden',
  85.                 'columns' => 6
  86.             ),
  87.            
  88.             array(
  89.                 'name'  => 'Address',
  90.                 'id'    => $prefix . 'address',
  91.                 'type'  => 'textarea',
  92.                 'columns' => 6
  93.             ),
  94.            
  95.             array(
  96.                 'name'  => 'Delivery Address',
  97.                 'id'    => $prefix . 'delivery_address',
  98.                 'type'  => 'textarea',
  99.                 'columns' => 6
  100.             ),
  101.            
  102.                        
  103.         ), // end customer fields
  104.     ); // end customer
  105.    
  106.     // Append phone or another field of customer to meta box above.
  107.     foreach ( $customers as $customer )
  108.     {
  109.         // Show this phone field if match name.
  110.         $meta_box['fields'][] = array(
  111.             'name'      => 'Phone',
  112.             'id'        => $prefix . 'phone_' . $customer->ID,
  113.             'type'      => 'text',
  114.             'std'       => get_post_meta( $customer->ID, 'p52_quote_phone', true ),
  115.             'visible'   => array( $prefix . 'name', $customer->ID )
  116.         );
  117.     }
  118.     $meta_boxes[] = $meta_box;
  119.  
  120.     return $meta_boxes;
  121. }
  122.  
  123. add_filter( 'rwmb_meta_boxes', 'p52_register_quote_contact_meta_boxes' );
  124.  
  125. // Save data to phone field
  126. add_action('rwmb_before_save_post', function($post_id)
  127. {
  128.     // Get person ID to save from "Select a Customer" field
  129.     $person_to_save = intval( $_POST['p52_quote_name'] );
  130.     // Save related field to phone field
  131.     $_POST['p52_quote_phone'] = $_POST['p52_quote_phone_' . $person_to_save];
  132.     // Unset all hidden fields
  133.     foreach ( $_POST as $key => $value )
  134.     {
  135.         if ( strpos( $key, 'p52_quote_phone_' ) )
  136.             unset( $_POST[$key] );
  137.     }
  138. } );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement