display menu only if logged in
-
Is there a way to hide or display menu items based on users logged in status. (based on the front end user plugin?)
-
Hi Valo,
There’s no easy way to do this currently. You’d have to do something similar to this:
https://wordpress.stackexchange.com/questions/7999/change-front-end-menu-depending-on-user-login
which is how to show a different menu depending on whether a user is logged in to the WordPress users portion of a site. You could use the plugin’s built-in FEUP_User class:
<?php $User = new FEUP_User(); if ($User->Is_Logged_In()) { // display logged in menu } else { // display not logged in menu } ?>
I ended up using the “if menu” plugin and added the FEUP option into that.
So I can easily select menu options from the admin.If you’re using the “if menu” plugin, you can just add your own menu conditions by following the code below:
// theme's functions.php or plugin file add_filter( 'if_menu_conditions', 'my_new_menu_conditions' ); function my_new_menu_conditions( $conditions ) { $conditions[] = array( 'name' => 'If single custom-post-type', // name of the condition 'condition' => function($item) { // callback - must return TRUE or FALSE return is_singular( 'my-custom-post-type' ); } ); return $conditions; }
Hi Valosip,
Could you provide the code for how you got the if menu plugin to display a conditional logout menu? I’m not sure how to get the “FEUP is logged in” option like in the screenshot you posted above.
add something like this in a functions.php or in a plugin.
It’s not perfect, but it allowed me to demonstrate the functionality for my friend. I’ll return to this later to clean it up, or wait and see if the devs of the plugin will add this into their code. Hope it helpsadd_filter( 'if_menu_conditions', 'my_new_menu_conditions' ); function my_new_menu_conditions( $conditions ) { if (function_exists("EWD_FEUP_Get_All_Users")) { $conditions[] = array( 'name' => 'FEUP is logged in', // name of the condition 'condition' => function($item) { $CheckCookie = CheckLoginCookie(); $currentUser = $CheckCookie['Username']; if( $currentUser ) return true; return false; } ); } return $conditions; }
Thanks, that worked out really well!
What if there are different levels for users. Say two levels in this case. Can you please provide some code so in the drop down list of the if menu. There’s a choice of, if user level 1, if user level 2, etc.
This would be really helpful to make a more administrative access to a menu shown to users who can access user lists and so on.
Is there a work-around of the code?
Many thanks!
/martinI don’t have the premium FEOP, i think user levels are only for premiums?
So I haven’t looked into that.
I’m assuming it might be possible though, if you check user levelTry the below. I quickly wrote it, it should work in theory. But im sure there is a better way.
You’ll need to add a different condition for each user level…let me know if it works
add_filter( 'if_menu_conditions', 'my_new_menu_conditions2' ); function my_new_menu_conditions2( $conditions ) { if (function_exists("EWD_FEUP_Get_All_Users")) { $conditions[] = array( 'name' => 'FEUP Level 1', // name of the condition 'condition' => function($item) { // callback - must return TRUE or FALSE global $wpdb; global $ewd_feup_user_table_name, $ewd_feup_levels_table_name; $FEUP = new FEUP_User; if($FEUP->Is_Logged_In()){ $Level_ID = $wpdb->get_var("SELECT Level_ID FROM $ewd_feup_user_table_name WHERE User_ID='" . $FEUP->Get_User_ID() . "'"); $Level = $wpdb->get_var("SELECT Level_Privilege FROM $ewd_feup_levels_table_name WHERE Level_ID='" . $Level_ID . "'"); } if( $Level == 1 ) return true; return false; } ); } return $conditions; }
yeap..
A front-end error appeard though,
/customers/7/a/4/martinhult.se/httpd.www/test/wp-content/plugins/if-menu/if-menu.php on line 100 Warning: call_user_func() expects parameter 1 to be a valid callback, no array or string given in /customers/7/a/4/martinhult.se/httpd.www/test/wp-content/plugins/if-menu/if-menu.php on line 100The FEUP User Level 1 appears in the statement though.
post your code. I ran this on my test site and it works
I’ll take a guess and say that you made an error in the format. It worked by adding one at a time with a new function and its working fine after following their plugin code:
<?php add_filter( 'if_menu_conditions', 'my_new_menu_conditions' ); function my_new_menu_conditions( $conditions ) { if (function_exists("EWD_FEUP_Get_All_Users")) { $conditions[] = array( 'name' => __( 'FEUP is logged in', 'if-menu' ), 'condition' => 'if_FEUP_is_logged_in' ); } if (function_exists("EWD_FEUP_Get_All_Users")) { $conditions[] = array( 'name' => __( 'FEUP Level 1', 'if-menu' ), 'condition' => 'if_FEUP_Level_1' ); } return $conditions; } function if_FEUP_is_logged_in() { $CheckCookie = CheckLoginCookie(); $currentUser = $CheckCookie['Username']; if( $currentUser ) return true; return false; } function if_FEUP_Level_1() { global $wpdb; global $ewd_feup_user_table_name, $ewd_feup_levels_table_name; $FEUP = new FEUP_User; if($FEUP->Is_Logged_In()){ $Level_ID = $wpdb->get_var("SELECT Level_ID FROM $ewd_feup_user_table_name WHERE User_ID='" . $FEUP->Get_User_ID() . "'"); $Level = $wpdb->get_var("SELECT Level_Privilege FROM $ewd_feup_levels_table_name WHERE Level_ID='" . $Level_ID . "'"); } if( $Level == 1 ) return true; return false; }
Works like a charm! =)
- The topic ‘display menu only if logged in’ is closed to new replies.