Timber Logo

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

Properties #

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

Methods #

NameReturn TypeSummary/Returns
__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.
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.
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: http://example.org/author/lincoln
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.
roles()array or nullGets the user roles.
slug()string

Returns: ex baberaham-lincoln

Class Methods #

__toString() #

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

Twig

This post is by {{ post.author }}

HTML

This post is by Jared Novack

Get the URL of the user's profile

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


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.

is_current() #

Check if the user object is the current user

Returns: bool true if the user is the current user


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


slug() #

Returns: string ex baberaham-lincoln


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.

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.

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 %}

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 %}

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 %}

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 %}

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 }) }}">