2

I'm trying to login users in wordpress through an API of another platform, so what I do is use an ajax function to call an external API sending the user credentials (username, password) and getting back the user info: (email, id, name...).

What I need is login the mentioned users in my wordpress website, but without save them (register them) in the wordpress database, so I'm not able to use any 'user role plugin' in order to administrate the visibility of pages/posts.

So, my goal is: Make pages/post and even menus only visibles for this external users when they are logged in my wordpress site, but without save them in the database.

I think that maybe using variable sessions or my own check_user_login() function, but I'm not sure how could I achieve this (where should I validate certain pages to set them visible only for logged in users?), because even if I have experience in PHP, it's my first time using wordpress.

I'm just asking for ideas, so it's because of that I'm not posting any code for now.

Thanks again for your help.

| improve this question | | | | |
  • 2
    Do users need a unique identity on your WordPress site? You could create a single subscriber and just log everyone in as that one user. – Milo May 13 '16 at 4:38
2

I think @Milo's comment is correct. Presumably you want to use functions like current_user_can(), etc. Alternatively, if this is just to show a different menu, you could create a custom menu for remote users then set a cookie on your wordpress site (via a call from the API) and display a different menu to your remote users when authenticated remotely.

add_filter('wp_nav_menu_args', 'wpse_wp_nav_menu_args_filter');
function wpse_wp_nav_menu_args_filter($args = array()) {
   if ($_COOKIE["remoteuser"] == "loggedin") { // User is remotely authenticated
        // Setup Primary Menu
        if ( $args['theme_location'] == 'primary' ) {
            $args['menu'] = "remote_user_menu";
        } 
        return $args;           
    }   else {
        return $args;       
            }
}

You could then use something along the same lines to determine whether a page should be visible or not.

| improve this answer | | | | |
  • Thank you so much to you and @Milo! Creating a single subscriber and just log everyone in as that one user, and saving the user info using sessions, and just overwriting some labels like the username in the admin bar etc, I have achieved what I needed. Now, it's much easier to hide pages or menus for that only suscriber user, just using available plugins. – esneyderp May 19 '16 at 3:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.