Uh oh!
There was an error while loading. Please reload this page.
Add youth member role - #73
Conversation
Implemented a youth member role which is assigned if the visitor is a member and has an age <=36.
cs1m0n
commented
Jan 16, 2023
Hey Steffan, The code looks fine to me, but I wonder if that's the way we actually want to go .. I fear that without a clear goal for this, we'll continue adding more and more roles and end up having no control over the logic .. Can you maybe describe the reason you need to identify youth members so we understand your use case? Only my 2 cents .. |
Hi @cs1m0n, as I have implemented it right now, the role only targets youth members from your local church. I agree with you that a more flexible logic is the way to go. We needed a quick-n-dirty solution to limit access of our youth meeting livestreams to youth members of our church. |
Uh oh!
There was an error while loading. Please reload this page.
steff-mueller
commented
Dec 27, 2023
I found a way to accomplish our use case without changing the
<?php/*Plugin Name: BCC Login Age RestrictionExtension plugin for https://github.com/bcc-code/bcc-wp*/constdwjz_post_id = 1758; // the id of the post where to apply the age restrictionconstdwjz_max_age = 36;
functiondwjz_on_template_redirect() {
// check post idif (get_the_ID() === dwjz_post_id && !dwjz_is_youth()) {
returndwjz_not_allowed_to_view_page();
}
}
add_action( 'template_redirect', 'dwjz_on_template_redirect', 1 );
functiondwjz_filter_menu_items( $items ) {
foreach ( $itemsas$key => $item ) {
if ($item->object_id == dwjz_post_id && !dwjz_is_youth()) {
unset( $items[ $key ] );
}
}
return$items;
}
add_filter( 'wp_get_nav_menu_items', 'dwjz_filter_menu_items', 21 );
/*Returns `true` if the current user's age is <= `dwjz_max_age`,otherwise the function returns `false`.*/functiondwjz_is_youth() {
// Check if user is logged in to Wordpressif (!is_user_logged_in()) {
returnfalse;
}
// Check if oidc token is availableif (!isset($_COOKIE['oidc_token_id'])) {
returnfalse;
}
// Check if oidc token is valid (still stored in transient)$token_id = $_COOKIE['oidc_token_id'];
if ( ! empty( $token_id ) ) {
$token = get_transient( 'oidc_id_token_' . $token_id );
}
if ( empty( $token ) ) {
returnfalse;
}
// Get token claims$claims = BCC_Login_Token_Utility::get_token_claims( $token );
$birthdate = date_create($claims['birthdate']);
$current_date = date_create();
$diff = date_diff($birthdate, $current_date);
$age = $diff->y;
return$age <= dwjz_max_age;
}
functiondwjz_not_allowed_to_view_page(){
wp_die(
sprintf(
'%s<br><a href="%s">%s</a>',
__( 'Sorry, you are not allowed to view this page.', 'bcc-login' ),
site_url(),
__( 'Go to the front page', 'bcc-login' )
),
__( 'Unauthorized' ),
array(
'response' => 401,
)
);
} |
victorienfotsing
commented
Dec 28, 2025
I have created a new WP plugin for this use case, offering more flexibility. It allows the administrator to define minimum and maximum age limits for each page whose content needs to be restricted. https://github.com/victorienfotsing/bcc-login-age-restriction |
Implemented a youth member role for content access which is assigned if the visitor is a member of your local church and has an age <=36.