A Cozmoslabs Product
Documentation / Custom Fields Creator / User Select Field

User Select Field

Overview

This Extra Field Type is available in WordPress Creation Kit Hobbyist or Pro. The User Select Field displays a dropdown that lets you select one of the users registered on your website.

Creating a User Select Field

To add a User Select Field to a previously created Custom Meta Box, under the Meta Box Fields tab simply insert a Field Title and under Field Type make sure you select User Select.

WordPress Creation Kit - Custom Fields Creator - Meta Box Fields - User Select Field

The User Select Field contains options to customize it:

  1. Description – Allows you to specify a description for the User Select Field
  2. Required – Select whether the field is required or not
  3. Default Value – Set a default value (username) for the User Select Field

Page, Post or Custom Post Type Edit Screen

This is how the User Select Field we created above looks like in the Edit Screen:

WordPress Creation Kit - Custom Fields Creator - Meta Box Fields - User Select Field (Editor)

Template usage

The following examples are for a Custom Meta Box with the “Group Name” argument “my_meta_name“. Make sure to replace this with the “Group Name” you have set up. The Custom Meta Box below is setup to be attached to a post.

WordPress Creation Kit - Custom Fields Creator - Meta Box Arguments - Template

Using the WCK Custom Fields API

The User Select Field returns the users display name or an array with the users details.

For a Single Meta Box

To output a value inside the loop we use the function the_cfc_field() which echoes the users display name

1
Name:<?php the_cfc_field('my_meta_name', 'teacher'); ?>

To assign the value to a variable we use the function get_cfc_field() which for the user select field returns an array with the users details:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?php 
$user = get_cfc_field('my_meta_name', 'teacher'); 
/*
$user contains the following:
array(11) {
  ["ID"] => "33"
  ["user_firstname"] => "John"
  ["user_lastname"] => "Smith"
  ["nickname"] => "J.Smith"
  ["user_nicename"] => "johnsmith"
  ["display_name"] => "John Smith"
  ["user_email"] => "jsmith@mail.com"
  ["user_url"] => "www.johnswebsite.com"
  ["user_registered"] => "2012-08-16 09:58:36"
  ["user_description"] => "Biology Teacher"
  ["user_avatar"] => "<img width="96" height="96" class="avatar avatar-96 photo" src="http://0.gravatar.com/avatar/avatar.jpg" alt="">"
}
*/
?>
For a Repeater Meta Box

To output all the “Teacher” entries from the repeater field we use the functions get_cfc_meta() and get_cfc_field():

1
2
3
4
5
<?php 				
foreach( get_cfc_meta( 'my_meta_name' ) as $key => $value ){
    the_cfc_field( 'my_meta_name','teacher', false, $key );
}
?>

To output a specific “Teacher” entry from the repeater field, for example the second entry, we use the function get_cfc_field():

1
Name: <?php the_cfc_field( 'my_meta_name','teacher', false, 1 ); ?>

The index starts at 0 so that’s why we pass “1” to the function. For the first entry it would be “0”, the second is “1”, the third is “2” and so on…

Using the WordPress default functions

The User Select Field returns the user id.

For a Single Meta Box
1
2
3
4
5
6
7
8
<?php 
$my_meta = get_post_meta( $post->ID, 'my_meta_name', true ); 
if( !empty( $my_meta[0]['teacher'] ) ){   
    $user_id = $my_meta[0]['teacher'];
    $user = get_user_by( 'id', $user_id );
    echo $user->first_name;
}
?>
For a Repeater Meta Box

To output all the “Teacher” entries in the repeater field:

1
2
3
4
5
6
7
8
9
10
<?php 
$my_meta = get_post_meta( $post->ID, 'my_meta_name', true );
if( !empty( $my_meta ) ){
    foreach( $my_meta as $entry ){       
        $user_id = $entry['teacher'];
        $user = get_user_by( 'id', $user_id );
        echo $user->first_name;
    }
}
?>

To output a specific “Teacher” entry from the repeater field, for example the second entry:

1
2
3
4
5
6
7
8
<?php 
$my_meta = get_post_meta( $post->ID, 'my_meta_name', true );
if( !empty( $my_meta[1]['teacher'] ) ){
    $user_id = $my_meta[1]['teacher'];
    $user = get_user_by( 'id', $user_id );
    echo $user->first_name;
}
?>

The index starts at 0 so that’s why we pass “1” to the function. For the first entry it would be “0”, the second is “1”, the third is “2” and so on…

The Ultimate Membership Bundle

Combine the power of Profile Builder with Paid Member Subscriptions to set up user registration, memberships, and recurring revenue.

Get 25% off with the bundle

Help & Support

We’re here to help you every step of the way.

Open a Support Ticket