How to use user metadata in post types via Advanced Custom Fields in WordPress

Henrik Bernström

To get the most out of this article a basic knowledge of PHP, WordPress, custom post types and Advanced Custom Fields is required.

If you've ever needed custom data input for your WordPress posts or pages (or any other kind of custom post type) then Advanced Custom Fields (ACF) is the shit! It's flexible, extensible and frequently improved by Elliot Condon. Simply brilliant!

I've been using ACF in a couple of projects now and in the current one I needed to be able to present WordPress users as hosts for different kinds of events, showing user a profile photo and a custom user metadata type. There was no obvious, flexible and easy to administer option available, or at least I couldn't find one, so I started thinking about what could be done with ACF and user profile custom fields. I decided to try it out.

Adding metadata to the user profile

To start with I had to add a couple of custom metadata fields to the user profile.

<?php
class JwCustomUserMeta {

	function __construct(){
		add_action( 'show_user_profile', array($this, 'jw_add_custom_user_profile_fields') );
		add_action( 'edit_user_profile', array($this, 'jw_add_custom_user_profile_fields') );
		add_action( 'personal_options_update', array($this, 'jw_save_custom_user_profile_fields')  );
		add_action( 'edit_user_profile_update', array($this, 'jw_save_custom_user_profile_fields')  );
	}

	function jw_add_custom_user_profile_fields( $user ) {
	?>

<?php _e('Extra Profile Information', 'your_textdomain'); ?>

<?php _e('Please enter your work title.', 'your_textdomain'); ?>
<?php _e('Please enter your phone number.', 'your_textdomain'); ?>
<?php } function jw_save_custom_user_profile_fields( $user_id ) { if ( !current_user_can( 'edit_user', $user_id ) ) return FALSE; update_usermeta( $user_id, 'jw_title', $_POST['jw_title'] ); update_usermeta( $user_id, 'jw_phone', $_POST['jw_phone'] ); } } ?>

Creating the User Meta field type

To be able to nicely select users as hosts in the custom post type Event I had to create a new Advanced Custom Field type; User Meta. It is in many ways a rip-off of the Select core field type differenting in the way that it is not configurable with respect to options (the values of the select input field) but the options are populated with all the registered WordPress users, instead.

Here's the code. I've kept most of the code in the create_field function to support ACF field options, that might be a requirement for some:

<?php

class acf_UserMeta extends acf_Field
{

	function __construct($parent)
	{
		// do not delete!
    	parent::__construct($parent);

    	// set name / title
    	$this->name = 'user_meta'; // variable name (no spaces / special characters / etc)
		$this->title = __("User Meta",'acf'); // field label (Displayed in edit screens)

   	}

	function create_field($field)
	{
		global $wpdb;

		// get the wordpress users
		$wp_user_search = $wpdb->get_results("SELECT ID, display_name FROM $wpdb->users ORDER BY display_name");

		$choices = array();

		// loop through users and add them as choices
		foreach ( $wp_user_search as $user ) {
			$user_id = (int) $user->ID;
			$display_name  = stripslashes($user->display_name);
			$choices[$user_id] = $display_name;
		}

		$field['choices'] = $choices;

		// defaults
		$field['value'] = isset($field['value']) ? $field['value'] : array();
		$field['multiple'] = isset($field['multiple']) ? $field['multiple'] : false;
		$field['allow_null'] = isset($field['allow_null']) ? $field['allow_null'] : false;
		$field['optgroup'] = isset($field['optgroup']) ? $field['optgroup'] : false;

		// no choices
		if(empty($field['choices']))
		{
			echo '

' . __("No choices to choose from",'acf') . '

';
			return false;
		}

		// multiple select
		$multiple = '';
		if($field['multiple'] == '1')
		{
			$multiple = ' multiple="multiple" size="5" ';
			$field['name'] .= '[]';
		} 

		// html
		echo '


';
	}
}
?>

So now, I can use this field as any other basic Advanced Custom Field; as standalone, in repeaters and flexible content. It's a very simple solution that stores the user id as value which can then be retrieved in templates to use when fetching information about the user via get_userdata($user_id);

Defining a User Meta field in the event ACF group.

Using the User Meta field to assign hosts to the event post type.

Implementing it in templates

Below I will list a piece of code where the field is used in a repeater, listing event hosts.

Hosted by

<?php while(the_repeater_field('event_hosts', $post->ID)): $user_id = get_sub_field('event_host', $post->ID); $user_info = get_userdata($user_id);?>
<?php echo $user_info->display_name; ?> <?php echo $user_info->jw_title; ?>
<?php endwhile; ?>

The result

The final result.

To learn more about how to create your own Advanced Custom Field types, see this video tutorial.

Henrik Bernström
Consultant at Jayway

Tags: , , , , , ,

0 comments ↓

There are no comments yet...Kick things off by filling out the form below.

Leave a Comment