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 #
Name | Type | Description |
---|---|---|
$avatar_override | string | A URL to an avatar that overrides anything from Gravatar, etc. |
$id | int | The ID from WordPress |
$user_nicename | string | |
$user_email | string | |
$roles | array |
Methods #
Name | Return Type | Summary/Returns |
---|---|---|
__toString() | string | Returns: a fallback for Timber\User::name() |
avatar() | string | Gets a user’s avatar URL. Returns: The avatar URL. |
can() | bool | Checks whether a user has a capability. Returns: Whether the user has the capability. |
can_edit() | bool | Checks whether the current user can edit the post. |
edit_link() | string or null | Gets 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. |
mixed | Gets a user meta value. Returns: The meta field value. | |
mixed | Gets a user meta value. Returns: The meta field value. | |
mixed | Gets a user meta value. Returns: The meta field value. | |
is_current() | bool | Check if the user object is the current user Returns: true if the user is the current user |
link() | string | Get the URL of the user's profile Returns: https://example.org/author/lincoln |
name() | string | Get the name of the User Returns: the human-friendly name of the user (ex: "Buster Bluth") |
path() | string | Get the relative path to the user's profile Returns: ex: /author/lincoln |
profile_link() | string or null | 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. Returns: The profile link for the current user. |
roles() | array or null | Gets 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
link() #
Get the URL of the user's profile
Returns: string
https://example.org/author/lincoln
get_field() #
Gets a user meta value.
DEPRECATED since 2.0.0, use {{ user.meta('field_name') }}
instead.
get_field( string $field_name = null )
Returns: mixed
The meta field value.
Name | Type | Description |
---|---|---|
$field_name | string | The 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.
Name | Type | Description |
---|---|---|
$field_name | string | The 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.
Name | Type | Description |
---|---|---|
$field_name | string | The 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 %}
profile_link() #
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.
Name | Type | Description |
---|---|---|
$capability | string | The capability to check. |
$args | mixed | Additional 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 %}
edit_link() #
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.
Name | Type | Description |
---|---|---|
$args | null or array | Parameters for get_avatar_url() . |
Get a user avatar with a width and height of 150px:
Twig
<img src="{{ post.author.avatar({ size: 150 }) }}">