Created
August 25, 2025 20:11
-
-
Save wpeasy/26fb9d42e0ebfdfab3f8feff0ee19947 to your computer and use it in GitHub Desktop.
Bricks Custom Role Based 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 | |
| /*############################## | |
| BRICKS BUILDER | |
| ###############################*/ | |
| /** | |
| * Bricks: add custom conditions | |
| * Requires Bricks Element Conditions API (Bricks 1.8.4+) | |
| */ | |
| // Define your condition config once | |
| define( 'WFH_CONDITIONS', [ | |
| 'wfh_user_is_admin_or_employee' => [ | |
| 'label' => 'User is Admin or Employee', | |
| 'roles' => [ 'administrator', 'employee' ], | |
| ], | |
| 'wfh_user_is_admin_or_employer' => [ | |
| 'label' => 'User is Admin or Employer', | |
| 'roles' => [ 'administrator', 'employer' ], | |
| ], | |
| 'wfh_user_is_admin_or_pro-member' => [ | |
| 'label' => 'User is Admin or Pro Member', | |
| 'roles' => [ 'administrator', 'member-level1' ], | |
| ] | |
| ] ); | |
| // 1) Register a custom group in Conditions UI | |
| add_filter( 'bricks/conditions/groups', function( $groups ) { | |
| $groups[] = [ | |
| 'name' => 'wfh_group', | |
| 'label' => esc_html__( 'WFH', 'wfh' ), | |
| ]; | |
| return $groups; | |
| } ); | |
| // 2) Register the condition options | |
| add_filter( 'bricks/conditions/options', function( $options ) { | |
| foreach ( WFH_CONDITIONS as $key => $data ) { | |
| $options[] = [ | |
| 'key' => $key, | |
| 'label' => esc_html__( $data['label'], 'wfh' ), | |
| 'group' => 'wfh_group', | |
| 'compare'=> [ | |
| 'type' => 'select', | |
| 'options' => [ | |
| '==' => esc_html__( 'is', 'wfh' ), | |
| '!=' => esc_html__( 'is not', 'wfh' ), | |
| ], | |
| 'placeholder' => esc_html__( 'is', 'wfh' ), | |
| ], | |
| 'value' => [ | |
| 'type' => 'text', | |
| 'placeholder' => esc_html__( 'ignored', 'wfh' ), | |
| ], | |
| ]; | |
| } | |
| return $options; | |
| } ); | |
| // 3) Provide the boolean result for our conditions | |
| add_filter( 'bricks/conditions/result', function( $result, $condition_key, $condition ) { | |
| $compare = isset( $condition['compare'] ) ? $condition['compare'] : '=='; | |
| $is_match = false; | |
| if ( is_user_logged_in() && isset( WFH_CONDITIONS[$condition_key] ) ) { | |
| $user = wp_get_current_user(); | |
| $roles = (array) $user->roles; | |
| foreach ( WFH_CONDITIONS[$condition_key]['roles'] as $role ) { | |
| if ( in_array( $role, $roles, true ) ) { | |
| $is_match = true; | |
| break; | |
| } | |
| } | |
| } | |
| return ( $compare === '!=' ) ? !$is_match : $is_match; | |
| }, 10, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment