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

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.
 
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.
 
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.
 
I think it will be easier for you to install an add on called NUNS (New User Notification System) by ragtek.
 
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.
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.
 
Let me build a quick example for you.

It won't take long.

Hi Chris,

I've written a few add-on that use the template hook. This was a great tutorial for that:

http://xenforo.com/community/resources/screencast-creating-xenforo-add-on.694/

But a template hook won't work for running php code after a user registers. I tried adding it to the register_confirm template but when my code ran it messed things up.

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.
 

Attachments

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.
 
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.
 

Attachments

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
 
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
 
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

if I recall correctly, if your class is sbr_register_DataWriter_User, then your directory should be, sbr/register/DataWriter/User.php.
 
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

Underscores in xenforo class names are essentially folder boundaries, so the User.php file should be in a subfolder as per the .zip
 
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.
 
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.
 
Back
Top Bottom