Created
October 6, 2025 10:45
-
-
Save goranefbl/ec29fa72ef6575ab3e913f95529f2683 to your computer and use it in GitHub Desktop.
Hide points per role
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 | |
/** | |
* Loyalty (WPGens) – gate features by ROLE instead of user meta. | |
* Default allowed roles: customer, subscriber. | |
* Change with: add_filter('wpgens_loyalty_allowed_roles', fn() => ['customer','vip']); | |
*/ | |
/** Helper: does user have any of the allowed roles? */ | |
function wpgl_user_has_allowed_role( $user_id ): bool { | |
if ( ! $user_id ) return false; | |
$user = get_user_by( 'id', $user_id ); | |
if ( ! $user instanceof WP_User ) return false; | |
$allowed_roles = apply_filters( 'wpgens_loyalty_allowed_roles', ['customer', 'subscriber'] ); | |
$allowed_roles = is_array( $allowed_roles ) ? array_map( 'strtolower', $allowed_roles ) : []; | |
$user_roles = array_map( 'strtolower', (array) $user->roles ); | |
return (bool) array_intersect( $allowed_roles, $user_roles ); | |
} | |
/** Only allow users with specific roles to earn points */ | |
add_filter( 'wpgens_loyalty_should_allow_points_for_user', function ( $should_allow, $user_id, $source, $type, $points_delta ) { | |
return wpgl_user_has_allowed_role( $user_id ); | |
}, 10, 5 ); | |
/** Show/hide the "points earning" box based on role */ | |
add_filter( 'wpgl_show_points_earning_box', function ( $should_show ) { | |
if ( is_user_logged_in() ) { | |
return wpgl_user_has_allowed_role( get_current_user_id() ); | |
} | |
// For guests keep whatever the plugin/theme default is: | |
return $should_show; | |
}, 10, 1 ); | |
/** Show/hide the redemption block based on role */ | |
add_filter( 'wpgens_loyalty_show_points_redemption_block', function ( $should_show ) { | |
if ( is_user_logged_in() ) { | |
return wpgl_user_has_allowed_role( get_current_user_id() ); | |
} | |
return $should_show; | |
}, 10, 1 ); | |
/** Add body classes so you can toggle UI with CSS (kept old class names for compatibility) */ | |
add_filter( 'body_class', function ( $classes ) { | |
if ( is_user_logged_in() ) { | |
$eligible = wpgl_user_has_allowed_role( get_current_user_id() ); | |
if ( $eligible ) { | |
$classes[] = 'loyalty-points-eligible-yes'; | |
$classes[] = 'gdpr-points-consent-yes'; // legacy class kept on purpose | |
} else { | |
$classes[] = 'loyalty-points-eligible-no'; | |
$classes[] = 'gdpr-points-consent-no'; // legacy class kept on purpose | |
} | |
} else { | |
$classes[] = 'loyalty-points-eligible-guest'; | |
$classes[] = 'gdpr-points-consent-guest'; // legacy class kept on purpose | |
} | |
return $classes; | |
}, 10, 1 ); | |
/** CSS: hide earning notice for ineligible users (keeps your previous selector working) */ | |
add_action( 'wp_head', function () { ?> | |
<style> | |
/* Old class kept working */ | |
.gdpr-points-consent-no .wpgens-points-earning-notice, | |
/* New explicit class */ | |
.loyalty-points-eligible-no .wpgens-points-earning-notice { | |
display: none !important; | |
} | |
</style> | |
<?php | |
}, 10 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment