Boolean Functions
Boolean functions help you validate data and make smart decisions in your templates. Instead of displaying broken content or causing errors, these functions let you check if data is valid, complete, and safe to use before showing it to users.
Why Boolean Functions Matter
Data validation challenges:
- User input that might be invalid email addresses
- Custom fields that could be empty or missing
- URLs that might be malformed or broken
- Numbers that could actually be text strings
What boolean functions solve:
- Input validation: Check emails before displaying contact forms
- Content safety: Verify URLs before creating links
- Conditional display: Show content only when data exists
- Error prevention: Validate data types before calculations
Data Type Checking
Verify that your data is the correct type before using it in calculations or displays.
is_numeric
Essential for validating prices, quantities, and any numeric input before calculations.
- Description
- Example
- Result
is_numeric(value)
When to use:
- Validate product prices before formatting
- Check user input quantities
- Verify numeric custom fields before calculations
# Validate product price input
is_numeric(user_entered_price)
# Check if rating is a number
is_numeric(wp.post.meta_value__rating)
# Verify quantity before calculation
is_numeric(item_quantity)
# "19.99" string → is numeric
true
# "4.5" rating → valid number
true
# "abc" text → not numeric
false
is_int
Perfect for validating counts, IDs, and anything that must be a whole number.
- Description
- Example
- Result
is_int(value)
When to use:
- Validate user IDs and post IDs
- Check quantity inputs (no half items)
- Verify array indices and counters
# Validate post ID
is_int(wp.post.ID)
# Check whole number quantity
is_int(cart_item_quantity)
# Verify user age input
is_int(user_age)
# 123 → valid integer
true
# 5 items → whole number
true
# 25.5 → not whole number
false
is_float
Verify that prices, ratings, and measurements are proper decimal numbers.
- Description
- Example
- Result
is_float(value)
When to use:
- Validate decimal prices and measurements
- Check rating values with decimals
- Verify percentage calculations
# Validate product price
is_float(product_price)
# Check star rating
is_float(average_rating)
# Verify percentage value
is_float(completion_percentage)
# 19.99 → valid decimal
true
# 4.5 stars → decimal rating
true
# 25 → integer, not float
false
Content Validation
Check if user input and content follows expected formats.
is_email
Ensure email addresses are properly formatted before displaying or processing them.
- Description
- Example
- Result
is_email(string)
Alias: isEmail
When to use:
- Validate contact form submissions
- Check user profile email addresses
- Verify author email fields before display
# Validate contact form email
is_email(contact_form_email)
# Check user profile email
is_email(wp.current_user.user_email)
# Verify author contact email
is_email(post_author_email)
# "[email protected]" → valid email format
true
# "[email protected]" → valid international format
true
# "invalid.email" → missing @ symbol
false
is_url
Check if URLs are properly formatted before creating links or displaying them.
- Description
- Example
- Result
is_url(string)
When to use:
- Validate website URLs before creating links
- Check social media profile links
- Verify custom field URLs before display
# Validate website URL
is_url(company_website)
# Check social media link
is_url(user_twitter_url)
# Verify product link
is_url(external_product_url)
# "https://example.com" → valid URL
true
# "www.example.com" → valid domain format
true
# "not-a-url" → invalid format
false
Empty and Null Checking
Verify that required data exists before trying to display or use it.
is_empty
Determine if fields contain actual content or are empty/null.
- Description
- Example
- Result
is_empty(value)
When to use:
- Check if required fields are filled
- Validate form submissions
- Determine if optional content should display
# Check if bio field has content
is_empty(user_bio)
# Validate required form field
is_empty(contact_message)
# Check if custom field exists
is_empty(wp.post.meta_value__custom_field)
# "" empty string → is empty
true
# null value → is empty
true
# "Some content" → has content
false
Common Validation Patterns
Safe Email Display
# Only show email if it's valid
is_email(contact_email) ? contact_email : 'Email not available'
# Create safe mailto link
is_email(user_email) ? 'mailto:' ~ user_email : '#'
Safe Link Creation
# Only create link if URL is valid
is_url(website_url) ? '<a href="' ~ website_url ~ '">Visit Website</a>' : 'Website URL not available'
# Safe external link
is_url(external_link) and not is_empty(external_link) ? external_link : '/contact'