wp-lemon Docs

Helper Functions

Helper Functions

The following function documentation is automatically generated.

Functions

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()string[] or falseFunction to format phone numbers throughout our template.

Returns: Array with information about the phone number.
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 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.

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 singular name.

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

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',
]);

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.

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.

since 3.17.0

format_phone_number( string|int $number )

Returns: string[]|false Array with information about the phone number.

NameTypeDescription
$numberstring or intThe actual phone number.

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.

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.

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.

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> $platforms )

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

NameTypeDescription
$platformsarray<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.

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.
**link** <Uses the following plugin: https://github.com/smithfield-studio/acf-svg-icon-picker>
**since** 5.7.0
`get_svg_icon( string|null $filename )`
**Returns:** `string|false` The svg icon or false if not found.
<div class="table-responsive">
| Name | Type | Description |
| --- | --- | --- |
| $filename | `string` or `null` | The filename of the icon. |
</div>
**Twig**
```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 %}

Edit this page on GitHub