Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function p52_quote_post_type() {
- register_post_type( 'p52-quote',
- array(
- 'labels' => array(
- 'name' => _x('Quote', 'post type general name'),
- 'singular_name' => _x('Quote', 'post type singular name'),
- 'add_new' => _x('Add New', 'quote'),
- 'add_new_item' => __('Add New Quote'),
- 'edit_item' => __('Edit Quote'),
- 'new_item' => __('New Quote'),
- 'all_items' => __('All Quotes'),
- 'view_item' => __('View Quote'),
- 'search_items' => __('Search Quotes'),
- 'not_found' => __('Quote wasn\'t found'),
- 'not_found_in_trash' => __('Quote wasn\'t found in Trash'),
- 'parent_item_colon' => '',
- 'menu_name' => 'P52 Quotes'
- ),
- 'public' => true,
- 'publicly_queryable' => true,
- 'show_in_menu' => true,
- 'query_var' => true,
- 'supports' => array( 'title' ),
- 'capability_type' => 'post',
- 'rewrite' => true,
- 'menu_position' => 5,
- 'hierarchical' => false,
- 'menu_icon' => 'dashicons-admin-links',
- )
- );
- }
- add_action( 'init', 'p52_quote_post_type' );
- /***************************
- * 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_boxes[] = 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' => 'text',
- 'columns' => 6
- ),
- array(
- 'name' => 'Email',
- 'id' => $prefix . 'email',
- 'type' => 'text',
- 'columns' => 6
- ),
- array(
- 'name' => 'Phone',
- 'id' => $prefix . 'phone',
- 'type' => 'hidden',
- 'columns' => 6
- ),
- array(
- 'name' => 'Address',
- 'id' => $prefix . 'address',
- 'type' => 'textarea',
- 'columns' => 6
- ),
- array(
- 'name' => 'Delivery Address',
- 'id' => $prefix . 'delivery_address',
- 'type' => 'textarea',
- 'columns' => 6
- ),
- ), // end customer fields
- ); // end customer
- // 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_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] );
- }
- } );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement