Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function p52_register_quote_meta_boxes( $meta_boxes ) {
- $prefix = 'p52_quote_';
- // Get all customer custom post type posts
- $customers = get_posts( array(
- 'posts_per_page' => -1,
- 'post_type' => 'customer'
- ) );
- // Define main meta box
- $meta_box = array(
- 'id' => $prefix . 'customer',
- 'title' => 'Customer Info',
- // Should define post types to display in array.
- // If you want this displays in Post, then assign to post here
- 'pages' => array( 'post' ),
- 'context' => 'normal',
- 'priority' => 'low',
- 'fields' => array(
- array(
- 'name' => 'Name',
- 'id' => $prefix . 'name',
- 'type' => 'post',
- 'post_type' => 'customer',
- 'placeholder' => 'Select a Customer'
- ),
- // Real field
- array(
- 'name' => 'Phone',
- 'id' => $prefix . 'phone',
- 'type' => 'hidden'
- )
- )
- );
- // Append phone or another field of customer to meta box above.
- foreach ( $customers as $customer )
- {
- // Show this phone field if match name.
- $meta_box['fields'][] = array(
- 'name' => 'Phone',
- 'id' => $prefix . 'phone_' . $customer->ID,
- 'type' => 'text',
- 'std' => get_post_meta( $customer->ID, 'p52_quote_phone', true ),
- 'visible' => array( $prefix . 'name', $customer->ID )
- );
- }
- $meta_boxes[] = $meta_box;
- return $meta_boxes;
- }
- add_filter( 'rwmb_meta_boxes', 'p52_register_quote_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] );
- }
- } );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement