docs/v2/guides/functions.md
Timber makes it easy to work with PHP functions in your Twig templates. You have three options:
function() - Need to use a custom or WordPress function that isn't pre-registered? Use the function() helper to call it directly from your templates.Let's explore each approach.
Timber provides a wide range of built-in functions that are directly available in Twig templates without any additional setup.
These functions allow you to fetch posts, terms, users, and comments:
get_post() - Retrieve a single postget_posts() - Retrieve multiple postsget_image() - Retrieve an imageget_external_image() - Retrieve an external imageget_attachment() - Retrieve an attachmentget_attachment_by() - Retrieve attachment by specific criteriaget_term() - Retrieve a single termget_terms() - Retrieve multiple termsget_user() - Retrieve a single userget_users() - Retrieve multiple usersget_comment() - Retrieve a single commentget_comments() - Retrieve multiple commentsaction() - Execute WordPress actions, see Twig – WordPress Actionsshortcode() - Process WordPress shortcodes via do_shortcode()bloginfo() - Get WordPress site informationTimber supports multilingual sites and provides functions to facilitate translation in Twig templates. You can read more about these functions in the internationalization Guide:
__() - Translate texttranslate() - Translate text_e() - Echo translated text_n() - Translate with plural support_x() - Translate with context_ex() - Echo translated text with context_nx() - Translate with plural and context support_n_noop() - Register plural strings for translation_nx_noop() - Register plural strings with context for translationtranslate_nooped_plural() - Translate plural strings registered with _n_noop() or _nx_noop()function()If you need to call a PHP function that isn't available as a built-in Timber function, you can use the function() helper. This allows you to call any PHP function directly from your Twig templates.
For example, if you need to call wp_head() and wp_footer():
{# single.twig #}
<html>
<head>
<!-- Add whatever you need in the head, and then...-->
{{ function('wp_head') }}
</head>
<!-- etc... -->
<footer>
Copyright © {{ "now"|date('Y') }}
</footer>
{{ function('wp_footer') }}
</body>
</html>
You can also use fn('my_function') as an alias for function('my_function').
To pass arguments to a function, add them as additional arguments after the function name:
{# single.twig #}
<div class="embed-link">
{{ function('get_post_embed_url', post.id) }}
</div>
Important note about context: While this works in a single.twig file that retains the context of the current post, it may not work the same way in The Loop (like in archive.twig or index.twig). Functions like get_post_embed_url try to guess the post ID from the current post context. In archive or index templates, you need to explicitly pass the post ID:
{# index.twig #}
<div class="embed-link">
{{ function('get_post_embed_url', post.ID) }}
</div>
For better performance and cleaner template syntax, you can register your custom functions to be directly available in Twig. This is preferable to using function() for functions you use frequently.
Check out the Extending Twig Guide to learn how to make your own functions available in Twig.
The concept of Timber (and templating engines like Twig in general) is to prepare all the data before you pass it to a template. Some functions in WordPress echo their output directly, which can cause issues since the output would appear before your template is rendered.
There are two ways to work around this:
Helper::ob_function - If you want to capture the output as a string to use in your context, use Helper::ob_function.FunctionWrapper - If a function needs to be called exactly where you use it in your template (because it depends on certain global values), use FunctionWrapper:$context['my_custom_function'] = new FunctionWrapper('my_custom_function', $array_of_arguments);