How to Debug Tokens
Updated over a week ago

This article was written by Vasiliy Leytman on our Facebook Group.

So when you have a token that you think should work, but it doesn't, and let's say it returns nothing, you can try to debug it.

Debugging Complex Tokens with multiple Filters, Subfields, or IDs

The easiest way to do it is to start with the lowest level of complexity possible, so if you have something like [acf:birthday|something|then|somethingelse], first, check if the most minimal version e.g. [acf:birthday] works.

To conveniently check that, just drop a "Text Editor with Tokens" widget on your page and enter your token there — so it will update the results with whatever changes you make to it in real-time.

If you checked it, and it works, then add another layer,

e.g. [acf:birthday|something] till it stops working or starts giving you a result that you don't expect it to generate.

Checking Properties out of Objects

Some tokens return objects, like lists of posts, so you can't "easily" see their output as a string. To check that, just use var_dump in the end — so [query:posts|var_dump]

for example, will output a list of posts being returned as a long string of parameters and their values.

It's also a nice way to check which properties a specific object has as it will list all of them. To make an example, let's say you have an ACF file field token

[acf:filefield] that normally returns a file array. And you want to get a caption from it.

But when you use basic [acf:filefield] it returns you the file name. How to know what your token should look like to get a caption? Well, just add var_dump

and you will see a long output of the file array (see image).

Another way is to make a token like [acf:filefield:] which will also list that array.

If you examine it, you'll see that it has caption the field inside — so a token that outputs the caption is gonna be like [acf:filefield:caption]

Using A Function with Parameter

Then in some cases, you might want to use an extra filter function that does something to a field or expression you get with some basic token.

E.g. let's say you have a checkbox field that holds a list of checkbox choices, and you want to output it as a string like "check1 > check2 > check3".

If you try a basic token [acf:checkboxfield] it will output a string like "check1, check2, check3". How to make it work the way you want? Well, check first which object it returns by trying [acf:checkboxfield:]. It'll show you it's an array. Then, Google something like "PHP flattens array into the string". The very first search result will likely have the definition of the function you need, in this case implode.

So that you can make a token like [acf:checkboxfield:implode] but if you try that, it's not gonna work. What's the reason? If you check the list of arguments of the function, you'll notice that it takes two arguments. One of them is always provided by the preceding token part (e.g. acf:checkboxfield — the value from the field), and then the second one should be a delimiter you want to separate array items with. So if you rework your token [acf:checkboxfield|implode(' > ')] it will do exactly what's needed

Building on what was previously described, you can use any PHP functions WP supports — or even some of your plugins provide, to process data you get from your fields, queries, terms, etc.

So for example, if you have an LMS, you can search their API docs for a function that returns if the user is enrolled in a specific course — and use it as a part of a token for visibility conditions.

Or if you want to process dates from fields, you can use expressions with your field values and PHP functions to calculate age difference:


Here we get the date from the current date and from the ACF field, and then use the function strtotime that converts date string into seconds, which you can then use to do some date calculation (in this case we calculate a person's age by dividing that number of seconds by the number of seconds in a year, and then rounding the result using another, "floor" function.

The cool thing about that is the internet is full of examples of such function, and also WordPress has lots of them to process posts, terms, and other elements of it — like if you want to get 3 words of your post content, you can use function wp_trim_words
e.g. [post:post_content|wp_trim_words(3)]
You can find descriptions and usage examples for those functions in WP Codex.

Did this answer your question?