Skip to main content
All CollectionsDynamic Content for ElementorFeaturesDynamic Visibility
Dynamic Visibility - Triggers - Custom Condition
Dynamic Visibility - Triggers - Custom Condition
Updated over a week ago

The Custom Condition feature in the Dynamic Visibility extension allows you to manage complex situations with custom PHP code.
​To add a custom condition to Dynamic Visibility, your code must return a boolean value. Depending on the display mode set ("Hide" or "Show" the element when the condition is triggered), this condition will determine the element's visibility.

Code Examples

Here are some examples of how you might use the Custom Condition:

Check if Current Post is in an Array of IDs:

$allowed_posts = [1, 21, 37, 46, 55]; 
return in_array(get_the_ID(), $allowed_posts);

Check Multiple Post Metadata Values:

$metakey1 = get_post_meta($post->ID, 'metakey1', true); 
$metakey2 = get_post_meta($post->ID, 'metakey2', true);

return ($metakey1 == 'value1' && $metakey2 == 'value2');

Check Number of Comments in a Post:

return get_comments_number(get_the_ID()) > 10;

Check if Post was Recently Published (Last Week):

return (time() - get_post_time('U', true, get_the_ID()) < WEEK_IN_SECONDS);

Check if Post Belongs to a Specific Post Type:

return get_post_type(get_the_ID()) === 'my_post_type';

Check User's Role on an Integrated Forum (e.g., bbPress):

return bbp_get_user_role(get_current_user_id()) === 'bbp_keymaster';

Check if the User Has Purchased a Specific Product:

$purchased_product_ids = wc_get_customer_bought_products(get_current_user_id()); 

return in_array('specific_product_id', $purchased_product_ids);

Verify User's Total Spend is Over a Certain Amount:

$customer_orders = wc_get_orders(['customer_id' => get_current_user_id()]);

$total_spent = array_sum(array_map(function ($order) { return $order->get_total(); }, $customer_orders));

return $total_spent > 500; // Change 500 to your desired amount

Check if Cart Total Exceeds a Certain Value:

return WC()->cart->total > 100; // Change 100 to your desired cart total

Verify if a Specific Product is in the Cart:

$product_id = 25; // Change 25 to your desired product ID to check

return WC()->cart->find_product_in_cart(WC()->cart->generate_cart_id($product_id)) !== false;

Determine if Cart Has More Than a Certain Number of Items:

return WC()->cart->get_cart_contents_count() > 5; // Change 5 to your desired number of items

Verify if the User's Last Order was Above a Certain Amount:

$last_order = wc_get_customer_last_order(get_current_user_id()); 
return $last_order && $last_order->get_total() > 200; // Change 200 to your desired amount

Important Note

Our technical support does not provide customised code. These examples are provided for illustrative purposes only and should be adapted and tested by a developer before being implemented in a production environment.

Did this answer your question?