Show menu item only if user is logged In (not word press login)

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

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
Add a comment  | 

1 Answer 1

Reset to default 5

Use 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

相关推荐

  • Show menu item only if user is logged In (not word press login)

    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

    11小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信