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.
1 Answer
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.