Skip to main content
Timber

You are reading the documentation for Timber v2.x. Switch to the documentation for Timber v1.x.

Timber\​User

A user object represents a WordPress user.

The currently logged-in user will be available as {{ user }} in your Twig files through the global context. If a user is not logged in, it will be false. This will make it possible for you to check if a user is logged by checking for user instead of calling is_user_logged_in() in your Twig templates.

Twig

{% if user %}
Hello {{ user.name }}
{% endif %}

The difference between a logged-in user and a post author:

PHP

$context = Timber::context();

Timber::render( 'single.twig', $context );

Twig

<p class="current-user-info">Your name is {{ user.name }}</p>
<p class="article-info">This article is called "{{ post.title }}"
and it’s by {{ post.author.name }}</p>

HTML

<p class="current-user-info">Your name is Jesse Eisenberg</p>
<p class="article-info">This article is called "Consider the Lobster"
and it’s by David Foster Wallace</p>

Overview #

This class extends Timber\CoreEntity
This class implements Stringable

Properties #

NameTypeDescription
$avatar_overridestringA URL to an avatar that overrides anything from Gravatar, etc.
$idintThe ID from WordPress
$rolesarray
$user_emailstring
$user_nicenamestring

Methods #

NameReturn TypeSummary/Returns
__call()mixedMagic method dispatcher for meta fields, for convenience in Twig views.

Returns: The value of the meta field named $field if truthy, false otherwise.
__toString()string

Returns: a fallback for Timber\User::name()
avatar()stringGets a user’s avatar URL.

Returns: The avatar URL.
can()boolChecks whether a user has a capability.

Returns: Whether the user has the capability.
can_edit()boolChecks whether the current user can edit the post.
convert()Finds any WP_Post objects and converts them to Timber\Posts
edit_link()string or nullGets the edit link for a user if the current user has the correct rights or the profile link for the current user.

Returns: The edit URL of a user in the WordPress admin or the profile link if the user object is for the current user. Null if the current user can’t edit the user.
fetch_meta()mixedGets an object meta value.

Returns: The custom field value or an array of custom field values. Null if no value could be found.
get_field()mixedGets a user meta value.

Returns: The meta field value.
get_meta()mixedGets a user meta value.

Returns: The meta field value.
get_meta_field()mixedGets a user meta value.

Returns: The meta field value.
is_current()boolCheck if the user object is the current user

Returns: true if the user is the current user
link()stringGet the URL of the user's profile

Returns: https://example.org/author/lincoln
meta()mixedGets an object meta value.

Returns: The custom field value or an array of custom field values. Null if no value could be found.
name()stringGet the name of the User

Returns: the human-friendly name of the user (ex: "Buster Bluth")
path()stringGet the relative path to the user's profile

Returns: ex: /author/lincoln
profile_link()string or nullGets the profile link to the user’s profile in the WordPress admin if the ID in the user object is the same as the current user’s ID.

Returns: The profile link for the current user.
raw_meta()null or mixedGets an object meta value directly from the database.

Returns: The meta field value(s). Null if no value could be found, an empty array if all fields were requested but no values could be found.
roles()array or nullGets the user roles.
slug()string

Returns: ex baberaham-lincoln

Class Methods #

__call() #

Magic method dispatcher for meta fields, for convenience in Twig views.

Called when explicitly invoking non-existent methods on a Core object. This method is not meant to be called directly.

link https://secure.php.net/manual/en/language.oop5.overloading.php#object.call

__call( string $field, array $arguments )

Returns: mixed The value of the meta field named $field if truthy, false otherwise.

This method is inherited from \Timber\CoreEntity.

NameTypeDescription
$fieldstringThe name of the method being called.
$argumentsarrayEnumerated array containing the parameters passed to the function. Not used.

PHP

$post = Timber\Post::get( get_the_ID() );

update_post_meta( $post->id, 'favorite_zep4_track', 'Black Dog' );

Timber::render( 'rock-n-roll.twig', array( 'post' => $post ) );

Twig

{# Since this method does not exist explicitly on the Post class,
it will dynamically dispatch the magic __call() method with an argument
of "favorite_zep4_track" #}

<span>Favorite <i>Zeppelin IV</i> Track: {{ post.favorite_zep4_track() }}</span>

__toString() #

Returns: string a fallback for Timber\User::name()

Twig

This post is by {{ post.author }}

HTML

This post is by Jared Novack

avatar() #

Gets a user’s avatar URL.

since 1.9.1

avatar( null|array $args = null )

Returns: string The avatar URL.

NameTypeDescription
$argsnull or arrayParameters for get_avatar_url().

Get a user avatar with a width and height of 150px:

Twig

<img src="{{ post.author.avatar({ size: 150 }) }}">

can() #

Checks whether a user has a capability.

Don’t use role slugs for capability checks. While checking against a role in place of a capability is supported in part, this practice is discouraged as it may produce unreliable results. This includes cases where you want to check whether a user is registered. If you want to check whether a user is a Subscriber, use {{ user.can('read') }}. If you only want to check whether a user is logged in, you can use {% if user %}.

since 1.8.5

can( string $capability, mixed $args )

Returns: bool Whether the user has the capability.

NameTypeDescription
$capabilitystringThe capability to check.
$argsmixedAdditional arguments to pass to the user_can function

Give moderation users another CSS class to style them differently.

Twig

<span class="comment-author {{ comment.author.can('moderate_comments') ? 'comment-author--is-moderator }}">
{{ comment.author.name }}
</span>

can_edit() #

Checks whether the current user can edit the post.

Returns: bool

Twig

{% if user.can_edit %}
<a href="{{ user.edit_link }}">Edit</a>
{% endif %}

convert() #

Finds any WP_Post objects and converts them to Timber\Posts

convert( array|\Timber\CoreEntity $data )

This method is inherited from \Timber\CoreEntity.

NameTypeDescription
$dataarray or \Timber\CoreEntity

Gets the edit link for a user if the current user has the correct rights or the profile link for the current user.

since 2.0.0

Returns: string|null The edit URL of a user in the WordPress admin or the profile link if the user object is for the current user. Null if the current user can’t edit the user.

Twig

{% if user.can_edit %}
<a href="{{ user.edit_link }}">Edit</a>
{% endif %}

Get the profile URL for the current user:

Twig

{# Assuming user is the current user. #}
{% if user %}
<a href="{{ user.edit_link }}">My profile</a>
{% endif %}

fetch_meta() #

Gets an object meta value.

Returns a meta value or all meta values for all custom fields of an object saved in the object meta database table.

Fetching all values is only advised during development, because it can have a big performance impact, when all filters are applied.

fetch_meta( string $field_name = '', array $args = [], bool $apply_filters = true )

Returns: mixed The custom field value or an array of custom field values. Null if no value could be found.

This method is inherited from \Timber\CoreEntity.

NameTypeDescription
$field_namestringOptional. The field name for which you want to get the value. If no field name is provided, this function will fetch values for all custom fields. Default empty string.
$argsarrayAn array of arguments for getting the meta value. Third-party integrations can use this argument to make their API arguments available in Timber. Default empty array.

$apply_filtersboolWhether to apply filtering of meta values. You can also use the raw_meta() method as a shortcut to apply this argument. Default true.

get_field() #

Gets a user meta value.

DEPRECATED since 2.0.0, use {{ user.meta('field_name') }} instead.

see Timber\User::meta()

get_field( string $field_name = null )

Returns: mixed The meta field value.

NameTypeDescription
$field_namestringThe field name for which you want to get the value.

get_meta() #

Gets a user meta value.

DEPRECATED since 2.0.0, use {{ user.meta('field_name') }} instead.

get_meta( string $field_name )

Returns: mixed The meta field value.

NameTypeDescription
$field_namestringThe field name for which you want to get the value.

get_meta_field() #

Gets a user meta value.

DEPRECATED since 2.0.0, use {{ user.meta('field_name') }} instead.

get_meta_field( string $field_name )

Returns: mixed The meta field value.

NameTypeDescription
$field_namestringThe field name for which you want to get the value.

is_current() #

Check if the user object is the current user

Returns: bool true if the user is the current user


Get the URL of the user's profile

Returns: string https://example.org/author/lincoln


meta() #

Gets an object meta value.

Returns a meta value or all meta values for all custom fields of an object saved in the meta database table.

Fetching all values is only advised during development, because it can have a big performance impact, when all filters are applied.

meta( string $field_name = '', array $args = [] )

Returns: mixed The custom field value or an array of custom field values. Null if no value could be found.

This method is inherited from \Timber\CoreEntity.

NameTypeDescription
$field_namestringOptional. The field name for which you want to get the value. If no field name is provided, this function will fetch values for all custom fields. Default empty string.
$argsarrayAn array of arguments for getting the meta value. Third-party integrations can use this argument to make their API arguments available in Timber. Default empty array.

name() #

Get the name of the User

Returns: string the human-friendly name of the user (ex: "Buster Bluth")


path() #

Get the relative path to the user's profile

Returns: string ex: /author/lincoln


Gets the profile link to the user’s profile in the WordPress admin if the ID in the user object is the same as the current user’s ID.

since 2.1.0

Returns: string|null The profile link for the current user.

Get the profile URL for the current user:

Twig

{% if user.profile_link %}
<a href="{{ user.profile_link }}">My profile</a>
{% endif %}

raw_meta() #

Gets an object meta value directly from the database.

Returns a raw meta value or all raw meta values saved in the meta database table. In comparison to meta(), this function will return raw values that are not filtered by third- party plugins.

Fetching raw values for all custom fields will not have a big performance impact, because WordPress gets all meta values, when the first meta value is accessed.

since 2.0.0

raw_meta( string $field_name = '' )

Returns: null|mixed The meta field value(s). Null if no value could be found, an empty array if all fields were requested but no values could be found.

This method is inherited from \Timber\CoreEntity.

NameTypeDescription
$field_namestringOptional. The field name for which you want to get the value. If no field name is provided, this function will fetch values for all custom fields. Default empty string.

roles() #

Gets the user roles.

Roles shouldn’t be used to check whether a user has a capability. Use roles only for displaying purposes. For example, if you want to display the name of the subscription a user has on the site behind a paywall.

If you want to check for capabilities, use {{ user.can('capability') }}. If you only want to check whether a user is logged in, you can use {% if user %}.

since 1.8.5

Returns: array|null

Twig

<h2>Role name</h2>
{% for role in post.author.roles %}
{{ role }}
{% endfor %}

Twig

<h2>Role name</h2>
{{ post.author.roles|join(', ') }}

Twig

{% for slug, name in post.author.roles %}
{{ slug }}
{% endfor %}

slug() #

Returns: string ex baberaham-lincoln