Skip to main content

Dynamic Visibility - Triggers - Custom Condition

Our technical support does not provide customized 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.

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

Code Examples

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

Check if the 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 the Post was Recently Published (Last Week):

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

Check if the 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 the 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

You Don't Need to Use a Function, but If You Do, It Must Be Called

When creating a Custom Condition for Dynamic Visibility, writing a function is unnecessary. You can write code that directly returns true or false.

However, if you use a function, call it correctly. Otherwise, it will not produce any result.

Example of Incorrect Behavior

function check_condition() { 
// Code that checks something
}

In this example, the function is not called, so it does not return a boolean value (true or false), making the custom condition ineffective.

Example of Correct Behavior Without a Function

return get_comments_number(get_the_ID()) > 10;

Here, the code directly checks if the number of comments is greater than 10 and returns the result without the need for a function.

Example of Correct Behavior with a Function

return check_condition(); 

function check_condition() {
// Code that checks a condition
return get_comments_number(get_the_ID()) > 10;
}

In this example, the function is called correctly and returns a boolean value, ensuring the condition works as expected.

Did this answer your question?