How to Add a Custom Function to Tokens
Updated over a week ago

Know more about tokens here.

To add a custom function to a token you need to add a function in your file functions.php, where the function name will be the filter name.


It has a unique parameter that contains the Token result, it may be a string, an array, an object, or null.

function my_custom_token_filter($data) { 
// do whatever you want
return $data;
}

Parameters

The Filters may accept also parameters, the Token's value is automatically passed to the function as the first parameter (or last for some function, like explode).

For example,

  • substr(0,3) gets the first 3 characters of the returned string;

  • get_the_post_thumbnail('full') gets the full image, not the thumb.

Example

The below function will work as the filter for our token.

function mFunction($data, $data2) { 
return $data + $data2;
}

Now, if we have an ACF number field with a value of 50 and key "acf_number", the below custom token will return by computing the value based on function.

[acf:acf_number|mFunction(60)]

Returned value: 50+60 = 110

Note: To use filters, don't forget to use "|"

Did this answer your question?