Last active
June 28, 2025 14:25
-
-
Save mehrshaddarzi/ece847d3b31a9d8eaaac966c860d2444 to your computer and use it in GitHub Desktop.
elementor Add condition show/hide field
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Register custom Elementor display conditions. | |
* | |
* @param \Elementor\Core\Kits\Documents\Tabs\Conditions_Manager $conditions_manager | |
* @return void | |
*/ | |
function register_custom_elementor_conditions( $conditions_manager ) { | |
// A class that defines our custom condition | |
class Elementor_VIP_User_Condition extends \Elementor\Core\Kits\Documents\Tabs\Conditions\Condition { | |
// Set a unique name for the condition | |
public function get_name() { | |
return 'vip_user_status'; | |
} | |
// Set the label that will be displayed in the editor | |
public function get_label() { | |
return __( 'VIP User Status', 'elementor-pro' ); | |
} | |
// Set the group for the condition (e.g., 'general', 'post', 'archive') | |
public function get_group() { | |
return 'custom_user'; // We'll create a new group | |
} | |
// Define the controls for your condition in the editor | |
public function get_controls() { | |
$this->add_control( | |
'vip_status', | |
[ | |
'type' => \Elementor\Controls_Manager::TEXT, | |
'label' => __( 'VIP Status Is', 'elementor-pro' ), | |
'placeholder' => 'e.g., 2', | |
] | |
); | |
} | |
// The logic of the condition. Return true to show the element, false to hide. | |
public function check( $args ) { | |
// First, check if the user is even logged in. If not, don't show. | |
if ( ! is_user_logged_in() ) { | |
return false; | |
} | |
// Get the value the site editor entered in the condition's control | |
$required_status = $args['vip_status']; | |
// If the editor left the field empty, don't show the element. | |
if ( empty( $required_status ) ) { | |
return false; | |
} | |
// Get the current user's ID | |
$user_id = get_current_user_id(); | |
// Get the 'is_vip' meta value for the current user | |
// The 'true' argument ensures a single value is returned. | |
$user_vip_meta = get_user_meta( $user_id, 'is_vip', true ); | |
// Compare the user's meta value with the required status. | |
// Using '==' for loose comparison (e.g., '2' will match 2) | |
return ( $user_vip_meta == $required_status ); | |
} | |
} | |
// Register our new condition | |
$conditions_manager->register( new Elementor_VIP_User_Condition() ); | |
// Register our new group "Custom User" | |
$conditions_manager->get_groups_config_manager()->register_group( | |
'custom_user', | |
[ | |
'label' => __( 'Custom User', 'elementor-pro' ), | |
] | |
); | |
} | |
// Add the action to hook into Elementor | |
add_action( 'elementor/conditions/register', 'register_custom_elementor_conditions' ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Register custom Elementor display conditions with a Select control. | |
* | |
* @param \Elementor\Core\Kits\Documents\Tabs\Conditions_Manager $conditions_manager | |
* @return void | |
*/ | |
function register_custom_elementor_select_condition( $conditions_manager ) { | |
// A class that defines our custom condition for Membership Level | |
class Elementor_Membership_Level_Condition extends \Elementor\Core\Kits\Documents\Tabs\Conditions\Condition { | |
// Set a unique name for the condition | |
public function get_name() { | |
return 'membership_level'; | |
} | |
// Set the label that will be displayed in the editor | |
public function get_label() { | |
return __( 'سطح عضویت کاربر', 'elementor-pro' ); | |
} | |
// Set the group for the condition | |
public function get_group() { | |
return 'custom_user'; | |
} | |
// Define the controls for your condition in the editor | |
public function get_controls() { | |
// **اینجا تغییر اصلی اعمال شده است** | |
$this->add_control( | |
'level', // نام کنترل ما | |
[ | |
'type' => \Elementor\Controls_Manager::SELECT, // نوع کنترل به SELECT تغییر کرد | |
'label' => __( 'سطح مورد نظر', 'elementor-pro' ), | |
'default' => 'gold', // یک مقدار پیشفرض برای کنترل | |
'options' => [ | |
// 'value' => 'Label' | |
'bronze' => __( 'برنزی', 'elementor-pro' ), | |
'silver' => __( 'نقرهای', 'elementor-pro' ), | |
'gold' => __( 'طلایی', 'elementor-pro' ), | |
'platinum' => __( 'پلاتینیوم', 'elementor-pro' ), | |
], | |
] | |
); | |
} | |
// The logic of the condition. Return true to show the element, false to hide. | |
public function check( $args ) { | |
// First, check if the user is logged in | |
if ( ! is_user_logged_in() ) { | |
return false; | |
} | |
// Get the selected level from the dropdown control | |
$required_level = $args['level']; // از نام کنترل جدید استفاده میکنیم | |
// Get the current user's ID | |
$user_id = get_current_user_id(); | |
// Get the 'membership_level' meta value for the current user | |
$user_level_meta = get_user_meta( $user_id, 'membership_level', true ); | |
// Compare the user's meta value with the required level from the dropdown. | |
return ( $user_level_meta === $required_level ); | |
} | |
} | |
// Register our new condition | |
$conditions_manager->register( new Elementor_Membership_Level_Condition() ); | |
// Register our new group "Custom User" (if not already registered) | |
if ( ! isset( $conditions_manager->get_groups_config_manager()->get_groups()['custom_user'] ) ) { | |
$conditions_manager->get_groups_config_manager()->register_group( | |
'custom_user', | |
[ | |
'label' => __( 'کاربر سفارشی', 'elementor-pro' ), | |
] | |
); | |
} | |
} | |
add_action( 'elementor/conditions/register', 'register_custom_elementor_select_condition' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment