Timber Logo

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


This is used in Timber to represent users retrived from WordPress. You can call $my_user = new Timber\User(123); directly, or access it through the {{ post.author }} method.


$context['current_user'] = new Timber\User();
$context['post'] = new Timber\Post();
Timber::render('single.twig', $context);


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


<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

Overview #

Properties #

avatar_overridestringA URL to an avatar that overrides anything from Gravatar, etc.
descriptionstringThe description from WordPress
first_namestringThe first name of the user
last_namestringThe last name of the user
idintThe ID from WordPress

Methods #

avatarstring/\Timber\ImageThe avatar URL.
canboolWhether the user has the capability.
namestringthe human-friendly name of the user (ex: "Buster Bluth")
pathstringex: /author/lincoln
slugstringex baberaham-lincoln

Class Methods #

__construct #

__construct( bool/object/int/bool $uid=false )

returns: void


__toString #

__toString( )

returns: string a fallback for TimberUser::name()


This post is by {{ post.author }}


This post is by Jared Novack

avatar #

avatar( mixed/null/array $args=null )

returns: string/\Timber\Image The avatar URL.

Gets a user’s avatar URL.

$argsmixed/null/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 #

can( string $capability )

returns: bool Whether the user has the capability.

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

$capabilitystringThe capability to check.

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

get_custom #

get_custom( )

returns: array/null

get_meta_field #

get_meta_field( string $field_name )

returns: mixed


link( )

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

meta #

meta( string $field_name )

returns: mixed


name #

name( )

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

path #

path( )

returns: string ex: /author/lincoln

roles #

roles( )

returns: array/null

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


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


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


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

slug #

slug( )

returns: string ex baberaham-lincoln

This class extends \Timber\Core

This class implements \Timber\CoreInterface