Skip to content
Radoslav Georgiev edited this page Oct 21, 2018 · 4 revisions

Intro

The User location allows you to associate containers with WordPress users.

user-location

Usage through the interface

To add fields to users with the the Administration Interface, please follow these steps:

  1. If you are not on the "Add Container" screen already, locate the "Ultimate Fields" section in the administration area and click the "Add New" button on the top (next to the "Containers" title).
  2. Locate the "Locations" box.
  3. Click "User" from the bottom row.
  4. Adjust all other needed options. All of them are described further down this page.

Usage through PHP

The user location in Ultimate Fields is handled by the Ultimate_Fields\Location\User class. In order to use it, you can either create the new location object manually or let the container do it for you.

The constructor of the class looks like this:

public function __construct( $args = array() ) {

$args is an array, which allows you to set arguments without having to explicitly call the particular setter. For example, you can pass 'roles' => 'editor' instead of calling ->set_roles( 'editor' )

Manual creation

Create a new location by using the new keyword and assign it to the container through the add_location() method.

use Ultimate_Fields\Container;
use Ultimate_Fields\Location\User;

$container = Container::create( 'addresses' );

// Create a new location and add definitions to it
$location = new User();
$location->set_roles( 'editor' );

// Once the location has been fully set up, add it to the container
$container->add_location( $location );

Do not forget to use the correct namespace for the location class!

Automatic creation

You can also let the container create the location for you by providing the "user" string as the first parameter to add_location().

use Ultimate_Fields\Container;

Container::create( 'adresses' )
	->add_location( 'user', array(
		'roles' => 'editor'
	));

This method allows you to use method chaining and shortens the syntax, in order to make the code more readable.

Data retrival

To retrieve the values of fields, associated with the User location, $type parameter of all *_value functions should have the "user_XX" format with XX representing the ID of the user.

Examples:

$country = get_value( 'country', 'user_3' );

Options

There are a few options for the User location and all of them are listed below.

User Roles

You can conditionally show fields in user profiles based on the role of the user, which is being edited.

In the UI

uncheck "Show on all user roles" and you will see checkboxes, which allow you to toggle the roles that you need.

In PHP

use the roles as the roles argument in the $args array or pass them to the set_roles method.

Roles are an MVIE Rule

You can add multiple values and/or exclude values by appending a minus sign in front of them.

Learn more

use Ultimate_Fields\Location\User;

// Show only for the editor
$container->add_location( 'user', array(
	'roles' => 'editor'
));

// Do not show on editors and authors
$location = new User();
$location->set_roles(array( '-editor', '-author' ));
$container->add_location( $location );

Registration Form

If registration is enabled in your site's settings, you can add additional fields to the registration form.

In the UI,

you will see a checkbox, which enables showing fields there.

In PHP,

you can use the registration_form argument or the set_registration_form method:

use Ultimate_Fields\Location\User;

$container->add_location( 'user', array(
	'registration_form' => true;
));

// or

$location = new User();
$location->set_registration_form();
$container->add_location( $location );

Adding to the customizer

Ultimate FIelds can display containers as sections in the Customizer for various locations and the User location is one of them.

Please read the Adding fields to the Customizer article in order to learn how to display user fields in the cutomizer and to use their values afterwards.

Usage with the REST API

In the UI

you need to go the "REST API" tab and select which fields you want to include. You can also select if those fields are editable or not.

In PHP

Please read the REST API section section of the Container Settings article, as the REST functionality is directly controlled in containers.

Administration columns

The User location supports Administration Columns. Click the link to learn how to use them.

Clone this wiki locally