Skip to main content
Timber

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

Timber\​Helper

As the name suggests these are helpers for Timber (and you!) when developing. You can find additional (mainly internally-focused helpers) in Timber\URLHelper.

Overview #

Methods #

NameReturn TypeSummary/Returns
array_to_object()\stdClass
array_truncate()array
convert_wp_object()mixedConverts a WP object (WP_Post, WP_Term) into its equivalent Timber class (Timber\Post, Timber\Term).

Returns: Instance of equivalent Timber object, or the argument if no match is found
deprecated()voidTriggers a deprecation warning.
doing_it_wrong()Marks something as being incorrectly called.
error_log()voidOutput a value (string, array, object, etc.) to the error log
get_object_by_property()array or null
get_object_index_by_property()bool or int
get_wp_title()string
is_array_assoc()bool
is_true()bool
iseven()boolIs the number even? Let's find out.
isodd()boolIs the number odd? Let's find out.
ob_function()stringCalls a function with an output buffer. This is useful if you have a function that outputs text that you want to capture and use within a twig template.
osort()voidSorts object arrays by properties.
pluck()arrayPlucks the values of a certain key from an array of objects
start_timer()floatFor measuring time, this will start a timer.
stop_timer()stringFor stopping time and getting the data.
transient()mixedA utility for a one-stop shop for transients.
warn()voidTrigger a warning.
wp_list_filter()arrayFilters a list of objects, based on a set of key => value arguments.

Class Methods #

array_to_object() #

array_to_object( array $array )

Returns: \stdClass

NameTypeDescription
$arrayarray

array_truncate() #

array_truncate( array $array, int $len )

Returns: array

NameTypeDescription
$arrayarray
$lenint

convert_wp_object() #

Converts a WP object (WP_Post, WP_Term) into its equivalent Timber class (Timber\Post, Timber\Term).

If no match is found the function will return the initial argument.

convert_wp_object( mixed $obj )

Returns: mixed Instance of equivalent Timber object, or the argument if no match is found

NameTypeDescription
$objmixedWP Object to convert

deprecated() #

Triggers a deprecation warning.

If you want to catch errors like these in tests, then add the @expectedDeprecated tag to the DocBlock. E.g.: "@expectedDeprecated {{ TimberImage() }}".

see _deprecated_function()

deprecated( string $function, string $replacement, string $version )

Returns: void

NameTypeDescription
$functionstringThe name of the deprecated function/method.
$replacementstringThe name of the function/method to use instead.
$versionstringThe version of Timber when the function was deprecated.

doing_it_wrong() #

Marks something as being incorrectly called.

There is a hook 'doing_it_wrong_run' that will be called that can be used to get the backtrace up to what file and function called the deprecated function.

The current behavior is to trigger a user error if WP_DEBUG is true.

If you want to catch errors like these in tests, then add the @expectedIncorrectUsage tag. E.g.: "@expectedIncorrectUsage Timber::get_posts()".

see _doing_it_wrong()

since 2.0.0

doing_it_wrong( string $function, string $message, string $version )

NameTypeDescription
$functionstringThe function that was called.
$messagestringA message explaining what has been done incorrectly.
$versionstringThe version of Timber where the message was added.

error_log() #

Output a value (string, array, object, etc.) to the error log

error_log( mixed $error )

Returns: void


get_object_by_property() #

get_object_by_property( array $array, string $key, mixed $value )

Returns: array|null

NameTypeDescription
$arrayarray
$keystring

get_object_index_by_property() #

get_object_index_by_property( array $array, string $key, mixed $value )

Returns: bool|int

NameTypeDescription
$arrayarray
$keystring

get_wp_title() #

get_wp_title( string $separator = ' ', string $seplocation = 'left' )

Returns: string

NameTypeDescription
$separatorstring
$seplocationstring

is_array_assoc() #

is_array_assoc( array $arr )

Returns: bool

NameTypeDescription
$arrarray

is_true() #

is_true( mixed $value )

Returns: bool


iseven() #

Is the number even? Let's find out.

iseven( int $i )

Returns: bool

NameTypeDescription
$iintnumber to test.

isodd() #

Is the number odd? Let's find out.

isodd( int $i )

Returns: bool

NameTypeDescription
$iintnumber to test.

ob_function() #

Calls a function with an output buffer. This is useful if you have a function that outputs text that you want to capture and use within a twig template.

ob_function( callable $function, array $args = [null] )

Returns: string

NameTypeDescription
$functioncallable
$argsarray

PHP

function the_form() {
echo '<form action="form.php"><input type="text" /><input type="submit /></form>';
}

$context = Timber::context( [
'form' => Timber\Helper::ob_function( 'the_form' ),
] );

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

Twig

<h1>{{ post.title }}</h1>
{{ my_form }}

HTML

<h1>Apply to my contest!</h1>
<form action="form.php"><input type="text" /><input type="submit /></form>

osort() #

Sorts object arrays by properties.

osort( array $array, string $prop )

Returns: void

NameTypeDescription
$arrayarrayThe array of objects to sort.
$propstringThe property to sort by.

pluck() #

Plucks the values of a certain key from an array of objects

pluck( array $array, string $key )

Returns: array

NameTypeDescription
$arrayarray
$keystring

start_timer() #

For measuring time, this will start a timer.

Returns: float


stop_timer() #

For stopping time and getting the data.

stop_timer( int $start )

Returns: string

NameTypeDescription
$startint

PHP

$start = Timber\Helper::start_timer();
// do some stuff that takes awhile
echo Timber\Helper::stop_timer( $start );

transient() #

A utility for a one-stop shop for transients.

transient( string $slug, callable $callback, int $transient_time = '', int $lock_timeout = 5, bool $force = false )

Returns: mixed

NameTypeDescription
$slugstringUnique identifier for transient
$callbackcallableCallback that generates the data that's to be cached
$transient_timeint(optional) Expiration of transients in seconds
$lock_timeoutint(optional) How long (in seconds) to lock the transient to prevent race conditions
$forcebool(optional) Force callback to be executed when transient is locked

PHP

$context = Timber::context( [
'favorites' => Timber\Helper::transient( 'user-' . $uid . '-favorites' , function() use ( $uid ) {
// Some expensive query here that’s doing something you want to store to a transient.
return $favorites;
}, 600 ),
] );

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

warn() #

Trigger a warning.

warn( string $message )

Returns: void

NameTypeDescription
$messagestringThe warning that you want to output.

wp_list_filter() #

Filters a list of objects, based on a set of key => value arguments.

Uses WordPress WP_List_Util's filter.

since 1.5.3

wp_list_filter( array $list, string|array $args, string $operator = 'AND' )

Returns: array

NameTypeDescription
$listarrayto filter.
$argsstring or arrayto search for.
$operatorstringto use (AND, NOT, OR).