I am new to Drupal. I want to execute my custom function every time when user registers or logins to my site. How can I do this?

2 Answers 2


You can do this using the following hooks in your module.

  1. hook_user_login
  2. hook_user_register (In D7 we had user_insert hook.)


User Login Hook:

function mymodule_user_login($account){

   //your code here

User Register Hook:

function mymodule_user_insert(Drupal\Core\Entity\EntityInterface $entity){

   //your code here

  • Thanq @esafwan . Where i need to place this code. In theme file or what ?
    – MH9
    Jun 14, 2017 at 8:44
  • @Mithun You have put this in a module. What does the custom function do? You might be able to use rules module instead for some use cases.
    – esafwan
    Jun 14, 2017 at 15:31
  • 2
    How can i get form values like first name, last name, email and password in mymodule_user_insert
    – MH9
    Jun 19, 2017 at 10:00

You can also use rule module for user login/register.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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