Tokens

What are Tokens?

Tokens are specially formatted chunks of text that serve as placeholders for a dynamically generated value.
Here's a really simple example: You want to display a welcome message to user's of your site, and you want it to be personalized so how about you add their name and instead of just saying "Welcome", you can say "Welcome, Joe".

In order to avoid having to hard-code a welcome string for every single user of the site it would be nice to dynamically generate the string. So you use a one like the following, "Welcome, [user:display_name?Anonymous]". A string of static text that will be located and replaced with a dynamic value. 

This token is made up of a few parts, inside of the mandatory square brackets that signify that this is a token. 

  1. The first part user: in this case is the token type. Token types are used to group like things together into a namespace. User's for example have name, mail, and last-login properties. Posts have title, ID and author properties. Token type also plays an important role in determining what tokens are available in what context.
  2. The second part, after the : is the token itself. This signifies what value will be substituted into the string containing the token. :display_name in this case indicates we want the current user's username.

Generic placeholder substitution

In many cases, it's useful to allow users to define patterns or large chunks of text that contain programmatically derived values.
For example, from email messages addressed to a given user, or URL path aliases containing the title of a given post. 

Both examples require bits of data that vary each time the text is generated -- post titles, user ids, and so on. Rather than forcing users to embed ugly snippets of PHP, shortcodes or creating elaborate and bizarre UIs for configuring the patterns via the browser, it's most useful to give users a set of 'placeholder' tokens to place in their text.

Accessing data through Tokens

DCE provide access to all basilar object of WP

  • Post
  • User
  • Term
  • Option
  • WP_Query
  • System
  • Date
  • ACF
  • Specific variable


Token shortcode composition

  • [ (required, open tag)
  • post (required, or user or option or term or wp_query or special variable)
  • : field name (required, native fields and meta are supported)
  • : sub field key (optional and multiple, if field is Array or Object)
  • | ID (optional, INT, if omitted will use the current by page)
  • | filter (optional and multiple, you can manipulate text result with php e wp functions)
  • ? fallback text (optional, shown if field value is empty or null)
  • ] (required, close tag)


Some complete example:

Post

  • [post:title] - print current Post Title, native field of this object (you can also write it without the repetitive post_)
  • [post:_yoast_wpseo_metadesc] - print current Post Meta Description, a post meta field created by 3rd part plugin
  • [post:ID|get_the_post_thumbnail(full)?<img src="/img/placeholder.png" />] - print current Post Featured Image trough a core WP function with parameters or a placeholder image if post hasn't got the Featured Image
  • [post:date|strtotime|date_i18n("j F Y")] - print the current Post Date in custom format
  • [post:date|strtotime|sum(86400)|date_i18n("Y-m-d H:i:s")] - print the Post Date plus one day in default time format
  • [post:ID|get_the_terms("category")|wp_list_pluck("name")] - print the name of the category (or categories) of Current Post
ACF
  • [acf:my_acf_field] - print the value of ACF Field, where "my_acf_field" is the name of the field in ACF settings, it works also in rows of ACF Repeater
  • [acf:my_file:filesize|size_format(2)] - print the Human readable size (with 2 optional decimal) of a ACF File Field
  • [acf:my_acf_field:title] - print the label of ACF Field set in his configuration
  • [acf:my_acf_field:settings:choices|options] - use the choices of ACF Field Select set in his configuration as options in a Select Field of Elementor PRO Form
User
  • [user:first_name] - print logged User first name, or nothing if page is viewed from a non authenticated visitor
  • [user:last_name|concat('Mr ',true)] - print logged User last name with a prefix
  • [user:email|explode(@)|reset] - print only username of current User Email (for example support@dynamic.ooo)
  • [user:nicename|23|trim?Anonimous] - print trimmed User Nicename from user with ID 23, otherwise if user has no nicename or user not exist will print "Anonymous"
Author (alias for User)
  • [author:display_name] - print the name of Author of Current Post
Term
  • [term:name] - print Term name of Current Term or First Term of Current Post
  • [term:description|123] - print Term description of the Term with ID 123
  • [term:count|123] - print Post count related to Term with ID 123
  • [term:link|last] - print the url to Term Archive of the last term of current post
  • [term:txtarea|artist] - print the value of an ACF field (like a Textarea) stored in Term of custom "artist" Taxonomy related to current Post
  • [term:term_id|category|posts|reset|get_the_title] - print the Title of last published Post in same Taxonomy (Category)
  • [term:term_id|123|posts|options] - return all Posts related to Term 123 as Options for Elementor PRO Form
Option
  • [option:elementor_version] - print current version of the installed Elementor's plugin from WP system options
  • [option:www_user_roles:editor:name|strtoupper] - print uppercased name of Editor Role
WP Query
  • [wp_query:posts] - print the id (or ids) of posts in the displayed page (like in archive)

System

  • [system:get:my_query_var] - print the variable stored inside $_GET, also compatible with POST, SESSION, COOKIE and SERVER systems vars
  • [system:MY_CONSTANT] - print the value of a constant previously declared in code
Date
  • [date:+1 month] - print the date of a month from today in Wordpress default format
  • [date|Y m d] - print the date of today in custom format
  • [date:user:registered] - print the current User registration date in default Wordpress format
  • [date:post:date:+2 days|Y-m-d H:i:s] - print the current Post publish date + 2 days (optional) in standard datetime format

Custom var in Loop

  • [ROW:var1|trim] - print the variable ROW inside widgets with your own html structure

Still need help? Contact Us Contact Us