0

Is there a way to toggle a custom metabox based on a custom drop down selection for a custom post type?

What I mean is I would like my custom post "proposal" to display different metaboxes based on a selection. For example, I select Print from a drop down and it will display all my custom fields that are assigned to Print. When I select Website I would like to display different fields that are assigned to Website. When each field is selected I would like the previous selected field to hide. Each selection will display a few fields.

To display my custom post type

 $args = array(
      'labels'              =>  array(
      'name'                => __( 'Proposal Overview'),
      'singular_name'       => __( 'Proposal'),
      'add_new'             => __( 'Add New Proposal'),
      'add_new_item'        => __( 'Add New Proposal'),
      'edit_item'           => __( 'Edit Proposal'),
      'new_item'            => __( 'New Proposal'),
      'all_items'           => __( 'All Proposal'),
      'view_item'           => __( 'View Proposal'),
      'search_items'        => __( 'Search Proposal'),
      'not_found'           => __( 'No Proposal found'),
      'not_found_in_trash'  => __( 'No Proposal found in Trash'),
      'hierarchical'        => true,
      'parent_item_colon'   => '',
      'all_items'           => __('Proposal')
           ),
      'supports'                => array( 'title','revisions','comments','author','editor' ),
                'show_in_menu'          => 'edit.php?post_type=client_portal',
                'public'                => true,
        );      
        register_post_type( 'Proposal', $args );    

Metaboxes to display for print (still working on more metaboxes)

  add_filter( 'cmb_meta_boxes', 'cmb_sample_metaboxes' );
  function cmb_sample_metaboxes( array $meta_boxes ) {
  $prefix = '_cmb_';

 $meta_boxes['squid_metabox_venders'] = array(
    'id'         => 'squid_metabox_venders',
    'title'      => __( 'Vendor Information', 'cmb' ),
    'pages'      => array( 'vendors', ), // Post type
    'context'    => 'normal',
    'priority'   => 'high',
    'show_names' => true, // Show field names on the left
    // 'cmb_styles' => true, // Enqueue the CMB stylesheet on the frontend
    'fields'     => array(
        array(
            'name' => __( '<h2>Project Size</h2>', 'cmb' ),
            'desc' => __( 'Folded and flat', 'cmb' ),
            'id'   => $prefix . 'squid_title',
            'type' => 'title',
        ),
        array(
            'name' => 'Flat',
            'desc' => 'optional',
            'id' => $prefix . 'vendorflat',
            'type' => 'text_small'
        ),
        array(
            'name' => 'Folded',
            'desc' => 'optional',
            'id' => $prefix . 'vendorfolded',
            'type' => 'text_small'
        ),
        array(
            'name'    => __( 'Fold Type', 'cmb' ),
            'desc'    => __( '', 'cmb' ),
            'id'      => $prefix . 'test_select',
            'type'    => 'select',
            'options' => array(
                'none' => __( 'None', 'cmb' ),
                'flat' => __( 'Flat Fold', 'cmb' ),
                'halfFold'   => __( 'Half Fold', 'cmb' ),
                'zFold'     => __( 'Z Fold', 'cmb' ),
                'triFold'     => __( 'Tri Fold', 'cmb' ),
                'gateFold'     => __( 'Gate Fold', 'cmb' ),
            ),
        ),
        array(
            'name' => __( '<h2>Project Pages/Paper</h2>', 'cmb' ),
            'desc' => __( '', 'cmb' ),
            'id'   => $prefix . 'squid_title',
            'type' => 'title',
        ),
        array(
            'name' => 'Pages',
            'desc' => '',
            'id' => $prefix . 'vendorPages',
            'type' => 'text_small'
        ),
        array(
            'name' => 'Paper',
            'desc' => '',
            'id' => $prefix . 'vendorPaper',
            'type' => 'text_small'
        ),
        array(
            'name' => 'Color',
            'desc' => '',
            'id' => $prefix . 'vendorColor',
            'type' => 'text_small'
        ),
        array(
            'name' => 'Special Inks',
            'desc' => '',
            'id' => $prefix . 'vendorInks',
            'type' => 'text_small'
        ),
        array(
            'name' => 'Coating',
            'desc' => '',
            'id' => $prefix . 'vendorCoating',
            'type' => 'text_small'
        ),
        array(
            'name' => 'Turnaround',
            'desc' => '',
            'id' => $prefix . 'vendorTurnaround',
            'type' => 'text'
        ),
        array(
            'name' => __( '<h2>Invoice Specs</h2>', 'cmb' ),
            'desc' => __( 'Phone Number, email, and web address', 'cmb' ),
            'id'   => $prefix . 'squid_title',
            'type' => 'title',
        ),
    ),
);
  • I would use CSS to hide all metaboxes by default except for the metabox with the dropdown options in it. Then use jQuery to Show the metabox in question on the select event and then when you select another option later, to hide the previous one. You would need to add in some more code for us to help you out practically though as just your function for registering the CPT above is not enough to go on... – Matt Royal Jul 2 '14 at 16:48
  • I just added to my post what I am working on to display my metaboxes @MattRoyal – user3756781 Jul 2 '14 at 16:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.