What are tokens?
Tokens are a short little piece of text that allows you to get some type of data from your WordPress website, which can then be used in your API calls. Tokens can be used within the admin area of WPGetAPI to allow you to create API dynamic calls without having to write code.
Looking at the image below, we have some examples of what tokens look like in action.
How to use tokens
- A token must start with an opening bracket and end with a closing bracket
- The ‘type’ of token (see below) must be the first word ie. (date or (system or (post or (user
- The token can be used anywhere in the WPGetAPI admin areas
Types of tokens
There are 5 types of tokens that we can use:
- Post tokens
- User tokens
- System tokens
- Date tokens
- WooCommerce order tokens
- Lifter LMS order tokens
Let’s now look at each token and see the settings we can use for each token.
Post tokens
A post token allows you to get post data for either the current post/page/custom post/product or for any post/page/custom post/product that you pass the ID for. The data you can get includes everything in the posts table and the postmeta table – this includes data from WooCommerce products, Advanced Custom Fields and standard custom meta fields.
The format of a post token looks like this: (post:id of post:post field or custom field|date format)
- id of post – either ‘current’ for the current post id, or a number such as 123 to get the post with id of 123
- post field or custom field – can be a custom field key or the name of any column in the posts table such as post_title, post_author, post_date_gmt, post_parent etc
- date format – only used when retrieving a date field and can be any PHP date format
Examples:
(post:current:post_title)
– get the post title of the current post(post:current:post_date|Y-m-d)
– get the date of the current post in Y-m-d format(post:345:post_date|Y-m-d)
– get the date of the post with ID of 345 in Y-m-d format(post:current:_yoast_wpseo_metadesc)
– get the Yoast meta description from current post(post:12:permalink)
– get the permalink of post 12(post:current:_sku)
– get the value of the custom field ‘_sku’ for the current WooCommerce product
User tokens
A user token allows you to get user data for either the current user or for any user that you pass the ID for. The data you can get includes everything in the users table and the usermeta table (excluding the user_password field).
The format of a user token looks like this: (user:id of user:user field or custom field|date format)
- id of user – either ‘current’ for the current user id, or a number such as 123 to get the user with id of 123
- user field or custom field – can be a custom user key or the name of any column in the users table such as user_email, user_registered, display_name etc
- date format – only used when retrieving a date field and can be any PHP date format
Examples:
(user:current:display_name)
– get the display name of the current user(user:current:user_registered|Y-m-d)
– get the date that the current user registered in Y-m-d format(user:345:user_registered|Y-m-d)
– get the user registration date of user with ID 345 in Y-m-d format(user:current:user_api_key)
– get the value of the custom field ‘user_api_key’ for the current user
System tokens
A system token allows you to get PHP super global variables such as $_GET, $_POST, $_REQUEST, $_COOKIE, $_SESSION and $_SERVER.
Examples:
(system:cookie:wp-settings-time-2)
– uses $_COOKIE to get the value of a cookie named ‘wp-settings-time-2’(system:get:page_no)
– uses $_GET to get the value of the query variable named ‘page_no’(system:server:REMOTE_ADDR)
– uses $_SERVER to get the value of ‘REMOTE_ADDR’, which will be the current visitors IP address
Date tokens
A date token gets the time or date in whatever format you like, based on the current time and date
Examples:
(time)
– get the time in your current WordPress time format(date)
– get the date in your current WordPress date format(date:+1 month)
– get the date 1 month from now in your current WordPress date format(date:+1 month|Y-m-d)
– get the date 1 month from now in Y-m-d format(date:Y-m-d)
– get todays date in Y-m-d format
WooCommerce order tokens
A WooCommerce order token allows you to grab order data including user details when a new order is made at your WooCommerce store.
To use WooCommerce Order tokens, you need to add the shortcode for your API endpoint (copied from the WPGetAPI endpoint screen) onto your WooCommerce checkout page and include the attribute of on=’woocommerce_order’: [wpgetapi_endpoint api_id=mycrm' endpoint_id='customer' debug='true' on='woocommerce_order']
Adding this shortcode then captures each order and sends whatever data you like to your API. And this shortcode will only fire once per order, so if the customer reloads the confirmation page multiple times, it will still only fire the API on the first instance of the order.
WooCommerce orders are saved as a custom post type, so the format of a WooCommerce order token looks very similar to the post tokens: (woo:order:order field or order custom field|date format)
- order field or order custom field – can be a custom field key or the name of any column in the posts table such as post_title, post_author, post_date_gmt, post_parent etc
- date format – only used when retrieving a date field and can be any PHP date format
Examples:
(woo:order:post_title)
– get the post title of the current post(woo:order:post_date|Y-m-d)
– get the date of the order in Y-m-d format(woo:order:ID)
– get the ID# of the order(woo:order:_billing_email)
– get the email address of the user placing the order(woo:order:_billing_first_name)
– get the first name of the user placing the order(woo:order:_billing_last_name)
– get the last name of the user placing the order(woo:order:_order_total)
– get the order total
Lifter LMS order tokens
A Lifter LMS order token allows you to grab order data including user details when a new order is made at your Lifter LMS store.
To use Lifter LMS Order tokens, you need to add the shortcode for your API endpoint (copied from the WPGetAPI endpoint screen) onto your Lifter LMS course page and include the attribute of on=’lifter_lms_order’: [wpgetapi_endpoint api_id=mycrm' endpoint_id='customer' debug='true' on='lifter_lms_order']
You may also want to add format="no_display"
if you want to hide the resulting data that is returned from your API.
This shortcode will capture each order and send whatever data you like to your API. And this shortcode will only fire once per order, so if the customer reloads the confirmation page multiple times, it will still only fire the API on the first instance of the order.
Lifter LMS orders are saved as a custom post type, so the format of a Lifter LMS order token looks very similar to the post tokens: (lifter:order:order field or order custom field|date format)
- order field or order custom field – can be a custom field key or the name of any column in the posts table such as post_title, post_author, post_date_gmt, post_parent etc
- date format – only used when retrieving a date field and can be any PHP date format
Examples:
(lifter:order:_llms_product_title)
– get the product title(lifter:order:_llms_product_type)
– get the product type (ie. course)(lifter:order:post_date|Y-m-d)
– get the date of the order in Y-m-d format(lifter:order:_llms_order_key)
– get the order key(lifter:order:_llms_billing_email)
– get the email address of the user placing the order(lifter:order:_llms_billing_first_name)
– get the first name of the user placing the order(lifter:order:_llms_billing_last_name)
– get the last name of the user placing the order(lifter:order:_llms__total)
– get the order total