Template conditional logic

You are here:
< All Topics

Post to Telegram module of WP Telegram Pro supports conditional tags/macros in Message Template. The conditional logic 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.

Lets dive in:

Basic logic

Syntax

[if<condition>][<consequence>][<alternative>]

Explanation: Basic syntax is same as that of WP Telegram (Free version) and consists of three blocks, where each block is placed in opening and closing square brackets i.e. [].

  1. Condition block – which contains the macro/tag to check for the condition, preceded by if i.e. [if<condition>].
  2. Consequence block – which contains the text or a macro/tag that will be used if the value of the macro/tag in Condition block is not empty.
  3. Alternative block (optional) – which contains the text or a macro/tag that will be used if the value of the macro/tag in 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 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 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 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 regular price is 60 and 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 consequence and alternative block if you want.

💡 Tip: If you get empty lines when no values exist for a tag/macro, you can add {remove_line} to alternative block to completely remove that line.
[if{tags}][🏷️ {tags}][{remove_line}]
Which 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 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 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 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 field book_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 same as “>” operator, except that it includes the 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 same as “<” operator, except that it includes the 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 contins 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 username is jhon or jack, add the verified badge
  • NOT_IN
    This operator works same as “IN” operator except that is works as 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.

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.
Previous Polylang Post Language rules
Next WooCommerce Save Amount and Percent tags
Table of Contents

Ready to dive in? Buy NOW