You would have to read the role of the user and decide depending on it. Here you can find an example in the 1st answer: https://stackoverflow.com/questions/36720949/get-user-role-by-id-wordpress
Thread Starter
nyper
(@nyper)
please send me an example snippet
Thread Starter
nyper
(@nyper)
i try this yesterday but not working.
can u send me a code to echo”hello” if “customer” login to site?
plz help me
Please show your code so that one can tell you what you have written wrong there. So you would also learn something instead of simply taking over finished.
Thread Starter
nyper
(@nyper)
add_action('wp_login', 'myfunc');
function myfunc() {
$user_meta = get_userdata($user_id);
$user_roles = $user_meta->roles;
if (in_array("administartor", $user_roles)){
//run a code
}
}
i use this one but my login page is breaking Down and all login tries got failed message.
I guess the problem is in wp_login hook
The error is actually how you use the hook. Check the manual: https://developer.ww.wp.xz.cn/reference/hooks/wp_login/
It says that the hook has 2 parameters: $user_login and $user – you ignore them completely and try to retrieve user data with a non-existing variable $user_id.
The correct way would be to call the hook correctly:
add_action('wp_login', 'myfunc', 10, 2 );
Then adjust the function call accordingly:
function myfunc( $user_login, $user ) {
and then you can query the user by his ID:
$user_meta = get_userdata($user->ID);
Also note that you have a misspelling of “administartor” below. But this doesn’t matter for your problem, you should only fix it if you want to get further.