October 8, 201411 yr I keep getting this error:- Warning: in_array() [function.in-array]: Wrong datatype for second argument in /homepages/45/d430176851/htdocs/DynamicDance/wp-content/themes/parallelus-unite/functions.php on line 181 I am still learning PHP so struggling to see what I have done wrong. The code is:- function users_login_redirect($redirect_to, $request, $user) { if(in_array('administrator', $user->roles)) { return admin_url(); } else { return '/members-area/'; } } add_filter( 'login_redirect', 'users_login_redirect', 10, 3); Thank you in advance :-)
October 8, 201411 yr Maybe you can try and use a variable: $user_roles = $user->roles then your code will be: function users_login_redirect($redirect_to, $request, $user) { $user_roles = $user->roles if(in_array('administrator', $user_roles)) { return admin_url(); } else { return '/members-area/'; } } add_filter( 'login_redirect', 'users_login_redirect', 10, 3); Shot in the dark, the above. Maybe also try: if ( current_user_can( 'administrator' ) ) so you need: function users_login_redirect($redirect_to, $request, $user) { if ( current_user_can( 'administrator' ) ) { return admin_url(); } else { return '/members-area/'; } } add_filter( 'login_redirect', 'users_login_redirect', 10, 3);
October 8, 201411 yr The second one actually would return the login link, only for users who are administrators, is that what you need?
October 10, 201411 yr Author Hello Teodora, Thanks for your reply. I did try both codes, I got an error with the first and the second redirected all users to the members area. I have setup a custom user role, I was trying to target these only and not admin. I tried changing "administrator" to the custom role but it didn't work. The original code I posted works but displays the error on the login screen.
October 11, 201411 yr You're getting the error because you are not providing an array in which to search. You need to search within the individual users for the role of administrator (or your custom role) for example try: <?php $users= get_users(); var_dump($users[0]->roles); ?> then you can use inarray(); In your case you can use a php counter to loop through all of the users. Edited October 11, 201411 yr by mantis
Create an account or sign in to comment