Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /***************************
- * create contact metaboxes
- ***************************/
- function p52_register_quote_contact_meta_boxes( $meta_boxes ) {
- $prefix = 'p52_quote_';
- // Get all customer custom post type posts
- $customers = get_posts( array(
- 'posts_per_page' => -1,
- 'post_type' => 'p52-contact'
- ) );
- // start customer
- $meta_box = array(
- 'id' => $prefix . 'customer',
- 'title' => 'Customer Info',
- 'pages' => 'p52-quote',
- 'context' => 'normal',
- 'priority' => 'low',
- 'fields' => array(
- array(
- 'name' => 'Name',
- 'id' => $prefix . 'name',
- 'type' => 'post',
- 'post_type' => 'p52-contact',
- 'placeholder' => 'Select a Customer',
- 'columns' => 6
- ),
- array(
- 'name' => 'Company',
- 'id' => $prefix . 'company',
- 'type' => 'hidden',
- 'columns' => 6
- ),
- array(
- 'name' => 'Email',
- 'id' => $prefix . 'email',
- 'type' => 'hidden',
- 'columns' => 6
- ),
- array(
- 'name' => 'Phone',
- 'id' => $prefix . 'phone',
- 'type' => 'hidden',
- 'columns' => 6
- ),
- array(
- 'name' => 'Address',
- 'id' => $prefix . 'address',
- 'type' => 'hidden',
- 'columns' => 6
- ),
- array(
- 'name' => 'Delivery Address',
- 'id' => $prefix . 'delivery_address',
- 'type' => 'hidden',
- 'columns' => 6
- ),
- ), // end customer fields
- ); // end customer
- // Append phone or another field of customer to meta box above.
- foreach ( $customers as $customer )
- {
- // Show this email field if match name.
- $meta_box['fields'][] = array(
- 'name' => 'New Email',
- 'id' => $prefix . 'email_' . $customer->ID,
- 'type' => 'text',
- 'std' => get_post_meta( $customer->ID, 'p52_quote_email', true ),
- 'visible' => array( $prefix . 'name', $customer->ID ),
- 'columns' => 6
- );
- // Show this phone field if match name.
- $meta_box['fields'][] = array(
- 'name' => 'New Phone',
- 'id' => $prefix . 'phone_' . $customer->ID,
- 'type' => 'text',
- 'std' => get_post_meta( $customer->ID, 'p52_quote_phone', true ),
- 'visible' => array( $prefix . 'name', $customer->ID ),
- 'columns' => 6
- );
- // Show this company field if match name.
- $meta_box['fields'][] = array(
- 'name' => 'New Company',
- 'id' => $prefix . 'company_' . $customer->ID,
- 'type' => 'text',
- 'std' => get_post_meta( $customer->ID, 'p52_quote_company', true ),
- 'visible' => array( $prefix . 'name', $customer->ID ),
- 'columns' => 6
- );
- // Show this address field if match name.
- $meta_box['fields'][] = array(
- 'name' => 'New Address',
- 'id' => $prefix . 'address_' . $customer->ID,
- 'type' => 'textarea',
- 'std' => get_post_meta( $customer->ID, 'p52_quote_address', true ),
- 'visible' => array( $prefix . 'name', $customer->ID ),
- 'columns' => 6
- );
- // Show this delivery address field if match name.
- $meta_box['fields'][] = array(
- 'name' => 'New Delivery Address',
- 'id' => $prefix . 'delivery_address_' . $customer->ID,
- 'type' => 'textarea',
- 'std' => get_post_meta( $customer->ID, 'p52_quote_delivery_address_', true ),
- 'visible' => array( $prefix . 'name', $customer->ID ),
- 'columns' => 6
- );
- }
- $meta_boxes[] = $meta_box;
- return $meta_boxes;
- }
- add_filter( 'rwmb_meta_boxes', 'p52_register_quote_contact_meta_boxes' );
- // Save data to phone field
- add_action('rwmb_before_save_post', function($post_id)
- {
- // Get person ID to save from "Select a Customer" field
- $person_to_save = intval( $_POST['p52_quote_name'] );
- // Save related field to phone field
- $_POST['p52_quote_phone'] = $_POST['p52_quote_phone_' . $person_to_save];
- // Unset all hidden fields
- foreach ( $_POST as $key => $value )
- {
- if ( strpos( $key, 'p52_quote_phone_' ) )
- unset( $_POST[$key] );
- }
- // Save related field to company field
- $_POST['p52_quote_company'] = $_POST['p52_quote_company_' . $person_to_save];
- // Unset all hidden fields
- foreach ( $_POST as $key => $value )
- {
- if ( strpos( $key, 'p52_quote_company_' ) )
- unset( $_POST[$key] );
- }
- // Save related field to email field
- $_POST['p52_quote_email'] = $_POST['p52_quote_email_' . $person_to_save];
- // Unset all hidden fields
- foreach ( $_POST as $key => $value )
- {
- if ( strpos( $key, 'p52_quote_email_' ) )
- unset( $_POST[$key] );
- }
- // Save related field to address field
- $_POST['p52_quote_address'] = $_POST['p52_quote_address_' . $person_to_save];
- // Unset all hidden fields
- foreach ( $_POST as $key => $value )
- {
- if ( strpos( $key, 'p52_quote_address_' ) )
- unset( $_POST[$key] );
- }
- // Save related field to delivery address field
- $_POST['p52_quote_delivery_address'] = $_POST['p52_quote_delivery_address_' . $person_to_save];
- // Unset all hidden fields
- foreach ( $_POST as $key => $value )
- {
- if ( strpos( $key, 'p52_quote_delivery_address_' ) )
- unset( $_POST[$key] );
- }
- } );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement