1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

I'd like to run some php code after a member registers

Discussion in 'XenForo Development Discussions' started by AndyB, Mar 16, 2013.

  1. AndyB

    AndyB Well-Known Member

    I have enabled moderation and I'd like to run some php code after a member registers.

    My php code will send me an email notifying that I have a new registration and will contain information to help me determine if this is a valid registration or a spammer.

    Which Code Event Listener do I need to use?

    Thank you for your assistance.
     
  2. Chris D

    Chris D XenForo Developer Staff Member

    There are many add-ons out there already that will incredibly accurately prevent spammers (both bots and humans) and will make that decision for you...

    But, to answer your question, you will most likely want to look at doing this by extending XenForo_DataWriter_User using the load_class_datawriter event listener.

    The existing XenForo_DataWriter_User class contains a postSave function.

    This is called whenever user information is inserted using the DataWriter.

    The best way to handle this is to verify if the data is new (isInsert as opposed to isUpdate) so that emails are only sent to you once the record is inserted into the database.

    Also, consider using the built in XenForo_Mail class for sending e-mails.
     
  3. AndyB

    AndyB Well-Known Member

    Hi Chris,

    I very much appreciate your reply. I prefer to write my own code over installing addons.

    When you you say "extending XenForo_DataWriter_User using the load_class_datawriter event listener.", how would I do this?

    Is there a video that you know of that shows doing this? If not I'd love to get some help explaining the steps.

    Thank you.
     
  4. Nasr

    Nasr Well-Known Member

    I think it will be easier for you to install an add on called NUNS (New User Notification System) by ragtek.
     
  5. Nasr

    Nasr Well-Known Member

  6. Chris D

    Chris D XenForo Developer Staff Member

    This tutorial is probably the single best resource for learning the XenForo add-on system.

    It begins on a basic level (which is basically injecting code into existing templates) but this basic level should hopefully steer you in the right direction of making add-ons that extend other areas (such as the DataWriter etc.)

    http://xenforo.com/community/resour...-insert-tabs-in-profile-page-using-hooks.335/

    As well as the main tutorial, there are several additional parts in the "Updates" tab. They might also help.
     
    Nasr likes this.
  7. AndyB

    AndyB Well-Known Member

    Thank you, Nasr. I'll watch Kier's tutorial now.
     
  8. Chris D

    Chris D XenForo Developer Staff Member

    Yeah forgot about Kier's. It's also good - maybe better as it's a video.
     
    Nasr likes this.
  9. AndyB

    AndyB Well-Known Member

  10. Chris D

    Chris D XenForo Developer Staff Member

    Let me build a quick example for you.

    It won't take long.

    Ok. Attached is a basic skeleton for one way of doing what you want. I haven't tested it, nor is this 100% complete (you will need to create your own add-on in the Admin CP, set up Event Listeners etc. This particular code will work with the load_class_datawriter code event listener), but it should give you a rough idea.
     

    Attached Files:

    Nasr likes this.
  11. AndyB

    AndyB Well-Known Member

    Chris you're awesome. I'll look at your example now.
     
  12. AndyB

    AndyB Well-Known Member

    Currently my folder structure is like this:

    /library/sbr/register/index.php

    Where would I put Listener.php and User.php??

    Do both files go in this folder?

    /library/sbr/register/

    My index.php file contains the following:

    Code:
    <?php
     
    class sbr_register_index
    {
        public static function registerEmail($class, array &$extend)
        {
            my php code will go here
        }
    }
     
    ?>
    
    Thank you, Chris.
     
  13. Chris D

    Chris D XenForo Developer Staff Member

    Ok, now I have an idea of what structure you have set up already I have amended my example.

    My index.php file should replace yours (it's essentially the same thing) and put the DataWriter folder in the same folder as index.php.

    You will need to create a Code Event Listener in the Admin CP that listens to the load_class_datawriter event.

    The class will be sbr_register_index and the method will be registerEmail.

    If you're using my example then the code you need to execute after someone has registered should be done in the User.php file, within the example IF statement in there.
     

    Attached Files:

    rootsxrocks likes this.
  14. AndyB

    AndyB Well-Known Member

    Hi Chris,

    First a big thank you. This is helping me immensely.

    I'm pretty sure I have everything correct. When I try to register as a new user, instead of getting the page where I fill everything out I get the following:

    Fatal error: Class 'sbr_register_DataWriter_User' not found in /path/to/my/files/www/xenforo/library/XenForo/DataWriter.php on line 1981
     
  15. AndyB

    AndyB Well-Known Member

    Just to make sure, these two files are suppose to be in the same folder correct?

    /library/sbr/register/index.php

    and

    /library/sbr/register/User.php
     
  16. Nasr

    Nasr Well-Known Member

    if I recall correctly, if your class is sbr_register_DataWriter_User, then your directory should be, sbr/register/DataWriter/User.php.
     
  17. Luke F

    Luke F Well-Known Member

    Underscores in xenforo class names are essentially folder boundaries, so the User.php file should be in a subfolder as per the .zip
     
    Nasr likes this.
  18. AndyB

    AndyB Well-Known Member

    Thank you, Nasr and Luke. I'll change the folder path now and see if that fixes it.
     
  19. AndyB

    AndyB Well-Known Member

    It works!!

    My php file is being called when the new registration is complete. What a wonderful community Xenforo is. I hope to be able to help others in the future.

    Thank you, Chris, Nasr and Luke.
     
    Sheldon and Nasr like this.
  20. AndyB

    AndyB Well-Known Member

    Unfortunately I think there's a bug in the Xenforo code.

    From what I can tell, when the Code Event Listener is extended this way, it prevents information filled out during registration in Custom User Fields from being entered into the database.
     

Share This Page