Skip to main content
Timber

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

Timber\​Comment

The Timber\Comment class is used to view the output of comments. 99% of the time this will be in the context of the comments on a post. However you can also fetch a comment directly using its comment ID.

PHP

$comment = Timber::get_comment( $comment_id );

$context = [
'comment_of_the_day' => $comment
];

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

Twig

<p class="comment">{{comment_of_the_day.content}}</p>
<p class="comment-attribution">- {{comment.author.name}}</p>

HTML

<p class="comment">But, O Sarah! If the dead can come back to this earth and flit unseen around those they loved, I shall always be near you; in the garish day and in the darkest night -- amidst your happiest scenes and gloomiest hours - always, always; and if there be a soft breeze upon your cheek, it shall be my breath; or the cool air fans your throbbing temple, it shall be my spirit passing by.</p>
<p class="comment-attribution">- Sullivan Ballou</p>

Overview #

This class extends Timber\CoreEntity
This class implements Stringable

Properties #

NameTypeDescription
$IDint
$comment_IDint
$comment_authorstring
$comment_author_emailstring
$comment_contentstring
$comment_datestring
$idint
$post_idint
$user_idint

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()stringGets the content.
add_child()arrayAdds a child.

Returns: Comment children.
approved()boolIs the comment approved?
author()\Timber\UserGets the author.
avatar()bool or mixed or stringFetches the Gravatar.
can_edit()boolChecks whether the current user can edit the comment.
children()arrayGets the comment children.

Returns: Comments
content()stringGets the content.
convert()Finds any WP_Post objects and converts them to Timber\Posts
date()stringThe date for the comment.
depth()intAt what depth is this comment?
edit_link()string or nullGets the edit link for a comment if the current user has the correct rights.

Returns: The edit URL of a comment in the WordPress admin or null if the current user can’t edit the comment.
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 comment meta value.

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

Returns: The meta field value.
is_child()boolChecks if the comment is a child.
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.
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.
reply_link()stringEnqueue the WP threaded comments JavaScript, and fetch the reply link for various comments.
time()stringWhat time was the comment posted?
update_depth()Updates the comment depth.

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() #

Gets the content.

Returns: string


add_child() #

Adds a child.

add_child( \Timber\Comment $child_comment )

Returns: array Comment children.

NameTypeDescription
$child_comment\Timber\CommentComment child to add.

approved() #

Is the comment approved?

Returns: bool

Twig

{% if comment.approved %}
Your comment is good
{% else %}
Do you kiss your mother with that mouth?
{% endif %}

author() #

Gets the author.

Returns: \Timber\User

Twig

<h3>Comments by...</h3>
<ol>
{% for comment in post.comments %}
<li>{{comment.author.name}}, who is a {{comment.author.roles[0]}}</li>
{% endfor %}
</ol>

HTML

<h3>Comments by...</h3>
<ol>
<li>Jared Novack, who is a contributor</li>
<li>Katie Ricci, who is a subscriber</li>
<li>Rebecca Pearl, who is a author</li>
</ol>

avatar() #

Fetches the Gravatar.

avatar( int|mixed $size = 92, string $default = '' )

Returns: bool|mixed|string

NameTypeDescription
$sizeint or mixedSize of avatar.
$defaultstringDefault avatar URL.

Twig

<img src="{{comment.avatar(36,template_uri~"/img/dude.jpg")}}" alt="Image of {{comment.author.name}}" />

HTML

<img src="https://gravatar.com/i/sfsfsdfasdfsfa.jpg" alt="Image of Katherine Rich" />

can_edit() #

Checks whether the current user can edit the comment.

Returns: bool

Twig

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

children() #

Gets the comment children.

Returns: array Comments


content() #

Gets the content.

Returns: string


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

date() #

The date for the comment.

date( string $date_format = '' )

Returns: string

NameTypeDescription
$date_formatstringof desired PHP date format (eg "M j, Y").

Twig

{% for comment in post.comments %}
<article class="comment">
<p class="date">Posted on {{ comment.date }}:</p>
<p class="comment">{{ comment.content }}</p>
</article>
{% endfor %}

HTML

<article class="comment">
<p class="date">Posted on September 28, 2015:</p>
<p class="comment">Happy Birthday!</p>
</article>

depth() #

At what depth is this comment?

Returns: int


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

since 2.0.0

Returns: string|null The edit URL of a comment in the WordPress admin or null if the current user can’t edit the comment.

Twig

{% if comment.can_edit %}
<a href="{{ comment.edit_link }}">Edit</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 comment meta value.

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

see Timber\Comment::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_field() #

Gets a comment meta value.

DEPRECATED since 2.0.0, use {{ comment.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_child() #

Checks if the comment is a child.

Returns: bool


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.

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.

Enqueue the WP threaded comments JavaScript, and fetch the reply link for various comments.

reply_link( string $reply_text = 'Reply' )

Returns: string

NameTypeDescription
$reply_textstringText of the reply link.

time() #

What time was the comment posted?

time( string $time_format = '' )

Returns: string

NameTypeDescription
$time_formatstringof desired PHP time format (eg "H:i:s").

Twig

{% for comment in post.comments %}
<article class="comment">
<p class="date">Posted on {{ comment.date }} at {{comment.time}}:</p>
<p class="comment">{{ comment.content }}</p>
</article>
{% endfor %}

HTML

<article class="comment">
<p class="date">Posted on September 28, 2015 at 12:45 am:</p>
<p class="comment">Happy Birthday!</p>
</article>

update_depth() #

Updates the comment depth.

update_depth( int $depth = '' )

NameTypeDescription
$depthintLevel of depth.