I am working on a web app word press. I am new to word press So maybe you will feel that this question is silly, I tried everything but i don't want to use any plugin.
here is what I need to show the register/login menu item to non-logged-in users and profile/logout menu item to logged-in-users. But In my website I am not using word press login function wp_login() I am using web service to get the login response and then I am using sessions to control the user data and access the user data on a different page.
On profile pages I have done
if(!isset($_SESSION['user'])){
header("Location:http://localhost:8080/wordpress/login/");
}
else {
//page content
}
I have tried this, but its not working
function my_wp_nav_menu_args( $args = '' ) {
if( isset($_SESSION['user']) ) {
$args['menu'] = 'logged-in';
}
return $args;
}
add_filter( 'wp_nav_menu_args', 'my_wp_nav_menu_args' );
But how can i do this with menu items ??
I am working on a web app word press. I am new to word press So maybe you will feel that this question is silly, I tried everything but i don't want to use any plugin.
here is what I need to show the register/login menu item to non-logged-in users and profile/logout menu item to logged-in-users. But In my website I am not using word press login function wp_login() I am using web service to get the login response and then I am using sessions to control the user data and access the user data on a different page.
On profile pages I have done
if(!isset($_SESSION['user'])){
header("Location:http://localhost:8080/wordpress/login/");
}
else {
//page content
}
I have tried this, but its not working
function my_wp_nav_menu_args( $args = '' ) {
if( isset($_SESSION['user']) ) {
$args['menu'] = 'logged-in';
}
return $args;
}
add_filter( 'wp_nav_menu_args', 'my_wp_nav_menu_args' );
But how can i do this with menu items ??
Share Improve this question edited Aug 16, 2016 at 7:01 Bilal Zafar 1332 silver badges7 bronze badges asked Aug 16, 2016 at 6:14 user101081user101081 211 silver badge2 bronze badges 2- Use is_user_logged_in() to check if a user is logged in. – ngearing Commented Aug 16, 2016 at 6:32
- Please merge your accounts. – fuxia ♦ Commented Aug 16, 2016 at 7:01
1 Answer
Reset to default 5Use the wp_nav_menu_objects filter to access the menu items
I have used this code:
add_filter('wp_nav_menu_objects', 'ad_filter_menu', 10, 2);
function ad_filter_menu($sorted_menu_objects, $args) {
// check for the right menu to rename the menu item from
// here we check for theme location of 'primary-menu'
// alternatively you can check for menu name ($args->menu == 'menu_name')
if ($args->theme_location != 'primary')
return $sorted_menu_objects;
// rename the menu item that has a title of 'Log ind'
if (is_user_logged_in()) {
foreach ($sorted_menu_objects as $key => $menu_object) {
// can also check for $menu_object->url for example
// see all properties to test against:
// print_r($menu_object); die();
if ($menu_object->title == 'Log in') {
$current_user = wp_get_current_user();
$menu_object->title = $current_user->user_login . " - Log out";
$menu_object->url = wp_logout_url();;
}
}
}
return $sorted_menu_objects;
}
Found the basis for that here: Remove a menu item in menu
Obviously you need to change the line
if (is_user_logged_in()) {
to
if( isset($_SESSION['user']) ) {
and you may need to check for additional menu items to change their title and url depending on status (Logged in or out)
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745081385a4610146.html
评论列表(0条)