Use variable set by another plugin?
-
Hello! I’m using a plugin ‘Restricted Site Access’ which sets a variable $is_restricted based on a whitelist of allowed IPs.
I’m trying to use that variable, plus ‘Nav Menu Roles’ to show/hide some menu items if user is logged in OR on the whitelist (ie $is_restricted is false).
Using code from a couple of your other support threads I’ve come up with this:
‘/* Add custom role to Nav Menu Roles menu list */
function kia_new_roles( $roles ){
$roles[‘restricted’] = ‘Restricted’;
return $roles;
}
add_filter( ‘nav_menu_roles’, ‘kia_new_roles’ );/*
* Change visibilty of each menu item
* param: $visible boolean
* param: $item object, the complete menu object. Nav Menu Roles adds its info to $item->roles
* $item->roles can be “in” (all logged in), “out” (all logged out) or an array of specific roles
* return boolean
*/
function kia_item_visibility( $visible, $item ){
if( isset( $item->roles ) && is_array( $item->roles ) && in_array( ‘restricted’, $item->roles ) ){
if ($is_restricted && ! is_user_logged_in() ){
$visible = true;
} else {
$visible = false;
}
}
return $visible;
}
add_filter( ‘nav_menu_roles_item_visibility’, ‘kia_item_visibility’, 10, 2 ); ‘The role ‘Restricted’ is added to menu items but seems to have no effect. Adding or removing my IP from the whitelist (which changes $is_restricted) also has no effect on the menu.
Can you tell me whether this approach is likely to work? My php is very limited and I don’t know if I’m wasting my time.
NB I have used $is_restricted successfully in another function to allow access to pages like this :
‘add_filter( ‘restricted_site_access_is_restricted’, ‘my_restricted_check’, 10, 2 );
function my_restricted_check( $is_restricted, $wp ) {
if ( $wp->query_vars[‘pagename’] == ‘contact’ ||
$wp->query_vars[‘name’] == ‘aplr-2013-volume-21-number-1-2’
)
$is_restricted = false;
return $is_restricted;
}’
- The topic ‘Use variable set by another plugin?’ is closed to new replies.