How Can We Help?
Template conditional logic
Post to Telegram module of WP Telegram (both Free and Pro versions) supports conditional tags/macros in Message Template. The free version supports only the Basic Logic and the Pro version not only supports the logic based upon the empty values for a tag/macro but also supports different operators that you can use to create a fully dynamic Message Template based on post category, tag, author, post type, custom fields, ACF fields, WooCommerce fields, etc.
Let us dive in:
Basic logic
Syntax
[if
<condition>
]
[
<consequence>
]
[
<alternative>
]
Explanation: Basic syntax (supported in both Free and Pro versions) consists of three blocks, where each block is placed in opening and closing square brackets i.e. []
.
- Condition block – which contains the macro/tag to check for the condition, preceded by
if
i.e.[if<condition>]
. - Consequence block – which contains the text or a macro/tag that will be used if the value of the macro/tag in the Condition block is not empty.
- Alternative block (optional) – which contains the text or a macro/tag that will be used if the value of the macro/tag in the Condition block is empty.
Examples
[if{tags}][🏷️ {tags}][]
It means if {tags}
have value, i.e., the post has tags added, then send 🏷️ {tags}
, otherwise don’t send anything.
[if{cf:_sale_price}][Sale price: ${cf:_sale_price}][]
It means if the custom field _sale_price
has a value, i.e., the product is on sale, then send Sale price: ${cf:_sale_price}, otherwise, don’t send anything. If the sale price is 40, then Sale price: $40 will be sent
Price: $[if{cf:_sale_price}][{cf:_sale_price}][{cf:_regular_price}]
Notice the value in the alternative block.
It means if the custom field _sale_price
has a value, then send Price: ${cf:_sale_price}, otherwise send the alternative block, i.e. Price: ${cf:_regular_price}.
[if{cf:_sale_price}][💰 Discounted Price: ${cf:_sale_price}][💵 Price: ${cf:_regular_price}]
Notice the extra text that you can add in the consequence and alternative block.
It means if the custom field _sale_price
has a value, i.e., the product is on sale, then send 💰 Discounted Price: ${cf:_sale_price}, otherwise send the alternative block, i.e., 💵 Price: ${cf:_regular_price}. If the regular price is 60 and the sale price is 55, then 💰 Discounted Price: $55 will be sent. If there is no sale price, 💵 Price: $60 will be sent.
Note: You can add new lines in both the consequence and alternative blocks if you want.
💡 Tip: If you get empty lines when no values exist for a tag/macro, you can add {remove_line}
to the alternative block to completely remove that line.[if{tags}][🏷️ {tags}][{remove_line}]
This means if there are no tags for the post, that whole line will be removed from the message template.
Advanced logic
Advanced logic is NOT available in the Free version but works much like the basic logic, but supports many different operators to use for comparison of values.
Syntax
[if<condition> <operator> <value>][<consequence>][<alternative>]
Explanation: The only difference from basic syntax is the operator and value in the condition block.
Supported operators:
- = (Equal)
This operator compares the values on both sides of the operator for equality. It should be used to compare singular values like post type, post author, custom fields, ACF fields, and WooCommerce fields.
Examples:
1.[if{cf:_regular_price} = 40][Grab at $40][]
i.e., if the product price is equal to 40, send Grab at $40, otherwise, send nothing.
2.<a href="{full_url}">[if{post_type} = product][Buy NOW][View post]</a>
i.e., if the post type is'product'
, then hyperlink text will be Buy NOW otherwise, View post
3.[if{wc:sku} = abcxyz][#featured][#regular]
i.e., if WooCommerce product SKU is abcxyz, add a hastag#featured
, otherwise, add hashtag#regular
- != (Not equal)
This operator compares the values on both sides of the operator for inequality. It should be used like “="
operator. It just does the opposite of the equality operator.
Examples:
1.[if{post_type} != product][{tags}][]
i.e., if it’s NOT a product then show tags, otherwise show nothing.
2.[if{post_author} != admin][From staff][From admin]
i.e., if post author username is NOT “admin” add From staff
- > (Greater than)
This operator checks if the value on the left (i.e. the macro value) is greater than the value on the right.
Examples:
1.[if{wc:price} > 500][FREE DELIVERY][STANDARD DELIVERY]
i.e., if the product price is greater than 500, show FREE DELIVERY, otherwise show STANDARD DELIVERY
2.[if{acf:book_price} > 999][FREE Gift wrap][{remove_line}]
i.e., if the ACF fieldbook_price
is greater than 999, show FREE Gift wrap, otherwise, remove the whole line.
- >= (Greater than or equal)
This operator checks if the value on the left (i.e., the macro value) is greater than or equal to the value on the right. It works the same as “>” operator, except that it includes the condition value in comparison.
- < (Less than)
This operator checks if the value on the left (i.e. the macro value) is less than the value on the right.
Examples:
1.[if{wc:price} < 100][#FMC][]
i.e., if the product price is less than 100, then add hashtag#FMC
- <= (Less than or equal)
This operator checks if the value on the left (i.e., the macro value) is less than or equal to the value on the right. It works the same as “<
” operator, except that it includes the condition value in comparison.
- CONTAINS
This operator checks if the value on the left (i.e. the macro value) contains the value/keyword on the right.
Examples:
1.[if{post_title} CONTAINS WordPress][#wp][{remove_line}]
i.e. if the post title contains the word “wordpress”, add the hashtag#wp
2.[if{wc:short_description} CONTAINS some text][Add something here][{remove_line}]
i.e. if product short description contains “some text”, then “Add something here”.
- NOT_CONTAINS
This operator checks if the value on the left (i.e. the macro value) does NOT contain the value/keyword on the right.
It works as opposite to “CONTAINS
” operator.
- IN
This operator checks if any of the macro values is in the comma-separated list on the right.
Examples:
1.[if{categories} IN news, magazine, breaking][Join our news channel on Telegram t.me/WPTelegram][{remove_line}]
i.e., if the post has any category from News, Magazines, or Breaking, then add the join link. Please check the notes below.
2.[if{post_author} IN jhon,jack][✅ Verified Author][{remove_line}]
i.e., if the post author’s username isjhon
orjack
, add the verified badge
- NOT_IN
This operator works the same as “IN
” operator except that it works as the opposite.
Examples:
1.[if{terms:product_cat} NOT_IN washer, freezer][🚛 1 Day Delivery][{remove_line}]
i.e., if the product is NOT in Washer or Freezer category, then add 1-day delivery badge
- BETWEEN
This operator checks if the macro value is BETWEEN the comma-separated two numbers on the right. This operator should only be used for comparing numbers.
Examples:
1.[if{wc:price} BETWEEN 500,1500][Extra 10% OFF][{remove_line}]
i.e. if the product price is between 500 and 1500, then add 10% OFF text.
- NOT_BETWEEN
This operator works same as “BETWEEN
” operator but does the opposite.
- STARTS_WITH
This operator checks if the macro value starts with the value on the right.
Example:[if{post_title} STARTS_WITH Breaking][#Breaking][{remove_line}]
i.e., if the post title starts with “Breaking” (for example “Breaking News”), then add a hashtag#Breaking
.
- NOT_STARTS_WITH
This operator checks if the macro value does NOT start with the value on the right.
- ENDS_WITH
This operator checks if the macro value ends with the value on the right.
Example:[if{post_title} ENDS_WITH NEW][#Latest][{remove_line}]
i.e. if the post title starts with “NEW”, then add a hashtag#Latest
.
- NOT_ENDS_WITH
This operator checks if the macro value does NOT end with the value on the right.
IMPORTANT NOTES:
- When using
{post_author}
, you should use username for comparison - When using taxonomies like
{tags}
,{categories}
or{terms:produc_cat}
etc., you should use term slug.