Title: execute function only when admin login
Last modified: July 2, 2022

---

# execute function only when admin login

 *  [nyper](https://wordpress.org/support/users/nyper/)
 * (@nyper)
 * [3 years, 11 months ago](https://wordpress.org/support/topic/execute-function-only-when-admin-login/)
 * hi
    i want to execute a function when users login who not “customer” for example:
   if user “admin” or “editor” or.. and login to the site then run a function
 * i use few codes but not working
 * last code i use for this: (not working)
 *     ```
       add_action('wp_login', 'test');
   
       function test() {
       if (is_user_admin()) {
       //code
       }
       }
       ```
   
    -  This topic was modified 3 years, 11 months ago by [nyper](https://wordpress.org/support/users/nyper/).

Viewing 7 replies - 1 through 7 (of 7 total)

 *  Moderator [threadi](https://wordpress.org/support/users/threadi/)
 * (@threadi)
 * [3 years, 11 months ago](https://wordpress.org/support/topic/execute-function-only-when-admin-login/#post-15790829)
 * 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](https://stackoverflow.com/questions/36720949/get-user-role-by-id-wordpress)
 *  Thread Starter [nyper](https://wordpress.org/support/users/nyper/)
 * (@nyper)
 * [3 years, 11 months ago](https://wordpress.org/support/topic/execute-function-only-when-admin-login/#post-15791774)
 * please send me an example snippet
 *  Moderator [threadi](https://wordpress.org/support/users/threadi/)
 * (@threadi)
 * [3 years, 11 months ago](https://wordpress.org/support/topic/execute-function-only-when-admin-login/#post-15791782)
 * You’ll find it under the link mentioned above: [https://stackoverflow.com/questions/36720949/get-user-role-by-id-wordpress](https://stackoverflow.com/questions/36720949/get-user-role-by-id-wordpress)
 *  Thread Starter [nyper](https://wordpress.org/support/users/nyper/)
 * (@nyper)
 * [3 years, 11 months ago](https://wordpress.org/support/topic/execute-function-only-when-admin-login/#post-15791805)
 * i try this yesterday but not working.
    can u send me a code to echo”hello” if“
   customer” login to site?
 * plz help me
 *  Moderator [threadi](https://wordpress.org/support/users/threadi/)
 * (@threadi)
 * [3 years, 11 months ago](https://wordpress.org/support/topic/execute-function-only-when-admin-login/#post-15791811)
 * 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](https://wordpress.org/support/users/nyper/)
 * (@nyper)
 * [3 years, 11 months ago](https://wordpress.org/support/topic/execute-function-only-when-admin-login/#post-15791814)
 *     ```
       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**
 *  Moderator [threadi](https://wordpress.org/support/users/threadi/)
 * (@threadi)
 * [3 years, 11 months ago](https://wordpress.org/support/topic/execute-function-only-when-admin-login/#post-15791823)
 * The error is actually how you use the hook. Check the manual: [https://developer.wordpress.org/reference/hooks/wp_login/](https://developer.wordpress.org/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.

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘execute function only when admin login’ is closed to new replies.

## Tags

 * [login](https://wordpress.org/support/topic-tag/login/)

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 7 replies
 * 2 participants
 * Last reply from: [threadi](https://wordpress.org/support/users/threadi/)
 * Last activity: [3 years, 11 months ago](https://wordpress.org/support/topic/execute-function-only-when-admin-login/#post-15791823)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
