The following Twig filters documentation is automatically generated.
Twig Filters
This document lists all custom Twig filters available in the WP Lemon theme. These filters extend Timber's functionality and provide convenient ways to transform data within Twig templates.
Available Filters
- antispambot
- towebpSrcset
- apply_filters_deprecated
- add_spaces_to_phonenumber
- textarea_to_array
- url_to_website_name
- ucfirst
antispambot
Converts email addresses to HTML entities to help prevent email address harvesting by spam bots.
This filter wraps the WordPress antispambot() function for use in Twig templates.
Parameters:
text(string) - The email address to encode
Returns: string|false - The encoded email address, or false if empty
Usage
{# Basic usage #}
{% set email = 'info@example.com'|antispambot %}
<a href="mailto:{{ email }}">{{ email }}</a>
{# With ACF field #}
{% set email = post.meta('email')|antispambot %}
{% if email %}
<a href="mailto:{{ email }}">{{ email }}</a>
{% endif %}
{# In person card #}
{% if post.meta('email') %}
{% set email = post.meta('email')|antispambot %}
<span class="crd__metaitem">
<i class="wp-lemon-icon-e-mail"></i>
<a href="mailto:{{ email }}" title="{{ __('Mail %s', 'wp-lemon')|format(post.title) }}">{{ email }}</a>
</span>
{% endif %}
towebpSrcset
Generate WebP srcset for responsive images with automatic conversion and optimization.
This filter creates a srcset attribute value with WebP versions of images at different sizes.
Since: 4.9.2
Parameters:
img(Timber\Image) - The image object
Returns: string|false - The srcset string with WebP images, or false if unavailable
Usage
{# Basic usage #}
<img src="{{ image.src }}" srcset="{{ image|towebpSrcset }}" sizes="(max-width: 768px) 100vw, 50vw" alt="{{ image.alt }}" />
{# With picture element for better control #}
<picture>
<source type="image/webp" srcset="{{ image|towebpSrcset }}" sizes="(max-width: 768px) 100vw, 50vw" />
<img src="{{ image.src }}" alt="{{ image.alt }}" />
</picture>
{# In a card loop #}
{% for post in posts %}
{% if post.thumbnail %}
<img src="{{ post.thumbnail.src('medium') }}" srcset="{{ post.thumbnail|towebpSrcset }}" alt="{{ post.title }}" />
{% endif %}
{% endfor %}
{# Hero image with WebP #}
{% set hero = get_image(fields.hero_image) %}
<div class="hero" style="background-image: url({{ hero.src('large') }})">
<img srcset="{{ hero|towebpSrcset }}" sizes="100vw" alt="{{ fields.hero_alt_text }}" />
</div>
Note: The WebP quality can be adjusted using the wp-lemon/filter/webp-quality filter (default: 97).
apply_filters_deprecated
Apply a deprecated WordPress filter with proper deprecation notice.
This filter allows you to use deprecated filters while properly logging deprecation notices for developers.
Since: 5.3.0
Parameters:
value(mixed) - The value to be filteredfilter_name(string) - The name of the deprecated filterargs(array) - Arguments to pass to the filterversion(string) - The version when the filter was deprecatedreplacement(string) - The filter that should be used instead
Returns: mixed - The filtered value
Usage
{# Apply deprecated filter #}
{% set title = post.title|apply_filters_deprecated('old_filter_name', [post], '5.0.0', 'new_filter_name') %}
{# With multiple arguments #}
{% set content = post.content|apply_filters_deprecated('wp-lemon/deprecated/content', [post.ID, post.type], '4.5.0', 'wp-lemon/filter/content') %}
{# Filtering excerpt with deprecated filter #}
{% set excerpt = post.excerpt|apply_filters_deprecated('theme_excerpt_filter', [150], '3.0.0', 'wp-lemon/filter/excerpt') %}
add_spaces_to_phonenumber
Add spaces to a phone number based on a custom pattern.
This filter formats phone numbers by adding spaces at specific positions, useful for displaying phone numbers in a readable format.
Since: 3.33.1
Parameters:
phone(string) - The phone number to formatpattern(array) - Array of integers defining where to add spaces. Default:[2, 2, 2, 2]
Returns: string - The formatted phone number with spaces
Usage
{# Basic usage with default pattern [2, 2, 2, 2] #}
{{ '0612345678'|add_spaces_to_phonenumber }}
{# Output: "06 12 34 56 78" #}
{# Custom pattern [2, 3, 3] #}
{{ '0612345678'|add_spaces_to_phonenumber([2, 3, 3]) }}
{# Output: "06 123 456 78" #}
{# Format international number #}
{{ '+31612345678'|add_spaces_to_phonenumber([3, 2, 3, 3]) }}
{# Output: "+31 61 234 5678" #}
{# Use with phonenumber function #}
{% set phone = phonenumber('+31180123456') %}
{% if phone %}
{# Apply custom spacing for service numbers #}
{{ phone.combined|add_spaces_to_phonenumber([3, 6, 2, 2]) }}
{# Output: "+31 (0)180 12 34 56" #}
{% endif %}
{# In a filter callback (used in PHP) #}
{# This is typically used within the wp-lemon/filter/phone-number/result filter #}
Common Patterns:
[2, 2, 2, 2]- Default: "06 12 34 56 78"[2, 3, 3]- Mobile: "06 123 456 78"[3, 2, 3, 3]- International mobile: "+31 61 234 5678"[3, 4, 2, 3]- Combined format: "+31 (0)6 12 345 678"[3, 6, 2, 2]- Service numbers: "+31 (0)180 12 34 56"
textarea_to_array
Convert a textarea field with line breaks into an array.
This filter is particularly useful for converting ACF textarea fields into arrays without needing to create repeater fields.
Since: 5.12.3
Parameters:
text(string) - The textarea content with line breaks
Returns: array|false - Array of lines, or false if empty
Usage
{# Basic usage #}
{% set usps = fields.usps|textarea_to_array %}
{% if usps %}
<ul>
{% for item in usps %}
<li>{{ item }}</li>
{% endfor %}
</ul>
{% endif %}
{# Benefits list #}
{% set benefits = fields.benefits_textarea|textarea_to_array %}
{% if benefits %}
<div class="benefits">
{% for benefit in benefits %}
<div class="benefit-item">
<i class="icon-check"></i>
{{ benefit }}
</div>
{% endfor %}
</div>
{% endif %}
{# Features with numbering #}
{% set features = fields.features|textarea_to_array %}
{% if features %}
<ol class="features-list">
{% for feature in features %}
<li>{{ feature }}</li>
{% endfor %}
</ol>
{% endif %}
{# Steps in a process #}
{% set steps = fields.process_steps|textarea_to_array %}
{% if steps %}
<div class="steps">
{% for step in steps %}
<div class="step">
<span class="step-number">{{ loop.index }}</span>
<p>
{{ step }}
</p>
</div>
{% endfor %}
</div>
{% endif %}
{# Conditional rendering #}
{% set items = fields.list_items|textarea_to_array %}
{% if (items|length) > 0 %}
<p>
We have {{ items|length }} items:
</p>
<ul>
{% for item in items %}
<li>{{ item }}</li>
{% endfor %}
</ul>
{% endif %}
ACF Setup: Create a textarea field in your field group, and users can enter one item per line. The filter will automatically convert it to an array.
url_to_website_name
Convert a URL to a readable website name by removing the protocol and trailing slash.
This filter is useful for displaying clean website names in links or references.
Since: 5.55.0
Parameters:
url(string) - The URL to convert
Returns: string - The website name without protocol and trailing slash
Usage
{# Basic usage #}
{{ 'https://www.example.com/'|url_to_website_name }}
{# Output: "www.example.com" #}
{# Display clean website names #}
{% set website = 'https://www.github.com/Studio-Lemon'|url_to_website_name %}
<p>
Visit us on {{ website }}
</p>
{# Output: "Visit us on www.github.com/Studio-Lemon" #}
{# Social media links #}
{% if fields.website_url %}
<a href="{{ fields.website_url }}" target="_blank">{{ fields.website_url|url_to_website_name }}</a>
{% endif %}
{# Multiple links #}
{% for link in fields.partner_websites %}
<div class="partner">
<a href="{{ link.url }}" target="_blank">{{ link.url|url_to_website_name }}</a>
</div>
{% endfor %}
{# External link with clean text #}
<a href="{{ post.meta('external_link') }}" target="_blank" rel="noopener">
{{ post.meta('external_link')|url_to_website_name }}
<i class="icon-external-link"></i>
</a>
{# Remove protocol from different schemes #}
{{ 'http://example.com'|url_to_website_name }}
{# Output: "example.com" #}
{{ 'https://subdomain.example.com/path'|url_to_website_name }}
{# Output: "subdomain.example.com/path" #}
ucfirst
Capitalize the first character of a string.
This filter wraps PHP's ucfirst() function for use in Twig templates.
Parameters:
text(string) - The string to capitalize
Returns: string - The string with the first character capitalized
Usage
{# Basic usage #}
{{ 'hello world'|ucfirst }}
{# Output: "Hello world" #}
{# Capitalize post type names #}
{{ post.type|ucfirst }}
{# If post.type is "job", output: "Job" #}
{# Capitalize user input #}
{% set name = fields.name|ucfirst %}
<h2>Welcome, {{ name }}!</h2>
{# In loops #}
{% for category in post.categories %}
<span class="category">{{ category.name|ucfirst }}</span>
{% endfor %}
{# Combined with other filters #}
{{ post.title|lower|ucfirst }}
{# Converts "HELLO WORLD" to "Hello world" #}
{# Capitalize first word in sentences #}
{% set description = fields.description|striptags %}
{{ description|ucfirst }}
{# Custom field labels #}
<label for="{{ field.name }}">{{ field.name|replace('_', ' ')|ucfirst }}:</label>
Note: This filter only capitalizes the first character. For title case (capitalizing each word), consider using additional logic or the title filter.