Skip to main content

Helper Functions

The following function documentation is automatically generated.

NameReturn TypeSummary/Returns
add_spaces_to_phonenumber()stringMarkup a phone number

Returns: the filtered phone number.
cache_clearer()voidCache clearer for spinupwp and wp-rocket.
format_phone_number()array{uri: string, whatsapp: string, timezone: string, countrycode: string, national: string, international: string, combined: string, localized: string} or falseFunction to format phone numbers throughout our template.
get_archive_page()int or falseRetrieves the archive page for a specific post type.

Returns: the page id or false if not found.
get_attachment_info()array or falseGet the attachment file info.

Returns: The attachment file info or false if not found.
get_constant()mixedGet constant value.

Returns: the value of the constant or false if not found.
get_fluent_form()string or falseReturn a fluent form.

Returns: The html of the form.
get_language_switcher()false or arrayCollects all languages and returns them as an array for twig language switcher.
get_shares()arrayBuilds the sharing links.

Returns: Array of shares that can be addressed by the share macro.
get_socials()arrayBuilds the social context.

Returns: Array of platforms that can be addressed by the share macro.
get_svg_icon()string or falseRetrieve a SVG icon from the acf-svg-icon-picker plugin.

Returns: The svg icon or false if not found.
get_svg_image()string or falseGet SVG image contents.

Returns: the attachment image svg data or false if not found.
is_post_type()Check if the current post type is one of the given post types.
log_message()voidAdds a log message to a specific log file in the website base folder.
post_type_name()string or falseGet singular name of a posttype.

Returns: The post type label, defaults to the singular name.
textarea_to_array()array or falseText helper to convert a textarea to an array.

Returns: The array of text or false if empty.
url_to_website_name()stringConvert a URL to a website name.

Returns: The website name.

get_constant()

Get constant value.

This function will return the value of a constant if it is defined.

get_constant( string $constant )

Returns: mixed the value of the constant or false if not found.

NameTypeDescription
$constantstringthe constant to get the value for.

post_type_name()

Get singular name of a posttype.

post_type_name( string $post_type, string $type = null )

Returns: string|false The post type label, defaults to the singular name.

NameTypeDescription
$post_typestringThe post type to check.
$typestringThe type of name to return, either 'plural' or 'singular'.

Twig

{{ post_type_name('job', 'plural') }}
{# Outputs "Jobs" #}

get_fluent_form()

Return a fluent form.

see FluentForm\App\Modules\Component\Component::renderForm function

get_fluent_form( string|int $id, string $theme = '', string $type = 'classic' )

Returns: string|false The html of the form.

NameTypeDescription
$idstring or intthe id of the form.
$themestringthe theme of the form, use 'ffs_inherit_theme' to inherit wp-lemon theme styling.
$typestringthe type of the form, can be 'classic' or 'conversational'.

Use this field in your ACF field group: PHP

->addField('form', 'fluentforms', [
'label' => __('Form', 'wp-lemon'),
'instructions' => __('Select the form you want to display.', 'wp-lemon'),
'return_format' => 'id',
]);

Then use it in your twig file like this: Twig

{{ get_fluent_form(post.meta('form'), 'ffs_inherit_theme', 'classic') }}

log_message()

Adds a log message to a specific log file in the website base folder.

You can use this function in your classes and functions to log errors and other messages.

since 3.19.5

log_message( string $prefix, string|array|object|null|bool|\WP_Error $message, string $filename = 'application', string|bool $special = false )

Returns: void

NameTypeDescription
$prefixstringThe prefix of the log message.
$messagestring or array or object or null or bool or \WP_ErrorThe message to log.
$filenamestringThe filename of the log file.
$specialstring or boolSpecial log message, can be 'first' or 'last' to add a special message.

PHP

log_message('Diagnostics cron', 'Starting diagnostics', special: 'first');
if (is_wp_error($response)) {
log_message('Diagnostic error', 'wp-error' . $response->get_error_message(), 'special': 'last');
return;
}

log_message('Diagnostics cron', 'Diagnostics completed', special: 'last');

Output in application.log:

/**
* Start new logging entry at 01-10-2025 13:32
* Initiated by /libary/my-cool-cron-file.php
*/
[01-10-2025 13:32] Diagnostics cron 🡆 Starting diagnostics
[01-10-2025 13:32] Diagnostics cron 🡆 Diagnostics completed
----------------------------------------

format_phone_number()

Function to format phone numbers throughout our template.

This array contains the following information:

  • uri: The uri of the phone number. Used in the tel link.
  • whatsapp: The whatsapp link of the phone number. Used to create a whatsapp link.
  • timezone: The timezone of the phone number.
  • countrycode: The country code of the phone number.
  • national: The national format of the phone number without the country code.
  • international: The international format of the phone number with the country code.
  • combined: The combined format of the phone number resulting in a format like +31 (0) 6 12345678.
  • localized: The localized format of the phone number. This is only used if WPML is active and the current language is not the default language.

Also available in Twig files via the phonenumber function.

since 3.17.0

format_phone_number( string|int $number )

Returns: array{uri: string, whatsapp: string, timezone: string, countrycode: string, national: string, international: string, combined: string, localized: string}|false

NameTypeDescription
$numberstring or intThe actual phone number.

PHP

$phone = format_phone_number('+31612345678');
if ($phone) {
echo '<a href="' . $phone['uri'] . '">' . $phone['localized'] . '</a>';
}

Or in Twig: Twig

{% set phone = phonenumber('+31612345678') %}
{% if phone %}
<a href="{{ phone.uri }}">{{ phone.localized }}</a>
{% endif %}

get_archive_page()

Retrieves the archive page for a specific post type.

This function will return the page id of the archive page for a specific post type. The page id can be set in the customizer. If WPML is active, the function will return the translated page id.

since 3.31.0 Helper function added.

get_archive_page( string $posttype )

Returns: int|false the page id or false if not found.

NameTypeDescription
$posttypestringthe posttype we are checking.

You can use this function to get the archive page for a specific post type. PHP

$archive_id = get_archive_page('job');
if ($archive_id) {
$archive = Timber::get_post($archive_id);
echo '<a href="' . $archive->link() . '">' . $archive->title() . '</a>';
}

Or even easier by using the WP_Lemon_Site::get_archive_page() method like this:

PHP

$archive = WP_Lemon_Site::get_archive_page('job');
if ($archive) {
echo '<a href="' . $archive['link'] . '">' . $archive['title'] . '</a>';
}

get_language_switcher()

Collects all languages and returns them as an array for twig language switcher.

since 3.18.0

Returns: false|array


get_svg_image()

Get SVG image contents.

Also available in Twig files via the get_svg_image function.

get_svg_image( int $attachment_id )

Returns: string|false the attachment image svg data or false if not found.

NameTypeDescription
$attachment_idintThe attachment ID.

add_spaces_to_phonenumber()

Markup a phone number

This function can be used to add a specific pattern of spaces to a phone number.

since 3.33.1

add_spaces_to_phonenumber( string $phone, array $pattern = [2, 2, 2, 2] )

Returns: string the filtered phone number.

NameTypeDescription
$phonestringthe phone number we want to filter.
$patternint[]the spacing pattern. You can input an array of numbers to add spaces after a specific amount of characters.

PHP

function filter_phone_numbers(array $result, int $countrycode): array
{

if (31 === $countrycode && str_starts_with($result['national'], '0180')) {
// add spaces after 3, 6, 2, 2 characters, so we have +31 (0)180 12 34 56.
$result['combined'] = add_spaces_to_phonenumber($result['combined'], [3, 6, 2, 2]);
}

return $result;
}
add_filter('wp-lemon/filter/phone-number/result', __NAMESPACE__ . '\\filter_phone_numbers', 11, 2);

get_shares()

Builds the sharing links.

This sets up the share content that is being used in the sharing macro.

get_shares( object|int|null $post = null )

Returns: array Array of shares that can be addressed by the share macro.

NameTypeDescription
$postobject or int or nullthe Post object or post id to get the share content for.

get_socials()

Builds the social context.

This sets up the social content that is being used in the social buttons macro.

get_socials( array<string,mixed> $platform_order )

Returns: array Array of platforms that can be addressed by the share macro.

NameTypeDescription
$platform_orderarray<string,mixed>platforms in an order we want to output the socials.

is_post_type()

Check if the current post type is one of the given post types.

since 4.4.0

is_post_type( string|array $post_type )

NameTypeDescription
$post_typestring or string[]The post type to check against.

Example usage:

PHP

if (is_post_type(['example', 'example2'])) {
// do something
}

cache_clearer()

Cache clearer for spinupwp and wp-rocket.

This function will clear the cache for the current post or the entire site.

since 4.8.0

cache_clearer( int|null $post_id = null )

Returns: void

NameTypeDescription
$post_idint or nullThe post id to clear the cache for.

get_attachment_info()

Get the attachment file info.

This function is also available in Twig files. See the example below.

This function will return the following information:

  • filename: The name of the file.
  • link: The link to the file.
  • extension: The file extension.
  • filesize: The size of the file in KB, MB or GB depending on the size.

Also available in Twig files via the get_attachment_info function.

since 5.4.0

get_attachment_info( int|null $attachment_id )

Returns: array|false The attachment file info or false if not found.

NameTypeDescription
$attachment_idint or nullThe attachment id to get the info for.

Twig

{% set attachment = get_attachment_info(attachment_id) %}
{% if attachment %}
<div class="attachment-info">
<h3>{{ attachment.filename }}</h3>
<p>({{ file_info.filesize }} / {{ file_info.extension }})</p>
<p>{{ attachment.extension }}</p>
<a href="{{ attachment.link }}">{{ attachment.link }}</a>
</div>
{% endif %}

get_svg_icon()

Retrieve a SVG icon from the acf-svg-icon-picker plugin.

This function will return the SVG icon from the resources/icons folder.

This function also works in Twig files. See the example below.

see [https://github.com/smithfield-studio/acf-svg-icon-picker](https://github.com/smithfield-studio/acf-svg-icon-picker Uses the following plugin)

since 5.7.0

get_svg_icon( string|null $filename )

Returns: string|false The svg icon or false if not found.

NameTypeDescription
$filenamestring or nullThe filename of the icon.

Twig

<div class="row">
{% for item in fields.repeater %}
<div class="icons-block__item col-md-4">
<div class="icons-block__icon">{{ get_svg_icon(item.icon) }}</div>
{{ item.text }}
</div>
{% endfor %}
</div>

textarea_to_array()

Text helper to convert a textarea to an array.

This can be used to convert a textarea ACF field with line breaks to an array. So you don't have to create a repeater field. This function can also be used as a filter inside your TWIG files.

since 5.12.3

textarea_to_array( string $text )

Returns: array|false The array of text or false if empty.

NameTypeDescription
$textstringThe text to convert.

Convert a textarea ACF field with line breaks to an array. Twig

{% set usps = fields.usps|textarea_to_array %}
{% if usps %}
<ul>
{% for item in usps %}
<li>{{ item }}</li>
{% endfor %}
</ul>
{% endif %}

url_to_website_name()

Convert a URL to a website name.

This function will remove the protocol and trailing slash from a URL.

since 5.55.0

url_to_website_name( string $url )

Returns: string The website name.

NameTypeDescription
$urlstringThe URL to convert.

PHP

$website_name = url_to_website_name('https://www.example.com/');
// $website_name will be 'www.example.com'