Nowadays, it is no surprise that user will usually receive an email when they first sign up for a new account with a site. It could be a greeting email or a confirmation email. Either way, this should be done automatically in the background. Imagine that a person in the back-end who needs to send an email to user every time when there is a new register. This could be a lot of work depends on the popularity of the site. Today, we are going to talk about how we can achieve that in WordPress. WordPress is a free and open-source content management system (CMS) based on PHP and MySQL. According to CMS Usage Statistics, it is reportedly the most popular blogging system in use on the web and it powers 26% of the website on the internet. really? One of the amazing feature that came pre-installed with WordPress is the ability to have multiple users, as an administrator of a WordPress site, you can set-up multiple users for the website and assign access levels and capabilities to each other. For example, you can assign role like editor to a specific person so that they can publish and manage all the posts on your site. But, who does not have the ability to install a new plugin on the site. If you would like to know all the roles that are available on pre-installed WordPress, please go ahead to their official site to check it out. After hearing many great things about WordPress, lets get to the point and start coding! Since WordPress is based on PHP in the back-end, we are going to code in PHP. If you do not know what a PHP is, you should take a look at some really basic things that can be done using PHP. PHP 5 tutorial by w3school.com. Beside PHP, we are going to use what so-called actions in WordPress. Actions are triggered by specific events that take place in WordPress, such as publishing a post, a new user sign up, or a user resetted their password for their account. An action is a custom PHP function defined in your plugin (or theme) and hooked, i.e. set to respond to specific event trigger by user. This is especially useful for programmer to write a clean and maintainable PHP code in WordPress. Which is again, another great feature came with WordPress. For more information about action in WordPress, please go to this site. The first step in creating an action in WordPress is to create a PHP function with the functionality that you want. In this case, we are going to write a PHP function that send a welcome email to user when this function get triggered. The code can be seen as follows,
PHP code for writing a function that send a welcome email to user
The above code should be fairly simple. We are using WordPress built-in function to send an email. For more information about it, please go to this site. Another thing that I would like to add is, in order to see the error message. You must set your WP_DEBUG_LOG to true in your wp-config.php file. On instruction on how to turn it on, please go to this site by WPMU DEV. Don’t forget that WP_DEBUG is for local development use and should not be used on production.
The following example that I would like to show is hook this function up with an action provided by WordPress. In this way, whenever there is a new user sign up, this function will get called automatically.
Full PHP code for sending a welcome email to user
source code hosted on GitHub
By hooking this function up with the action ‘user_register’, all we need to do is add one line of code and we are done. (please make sure you put this function into the core functions.php in your theme folder in order for it to work!).
Hopefully this guide has given you the confidence to do things with WordPress Action. Please go ahead and take a look at the list of all WordPress hook that they have offer. I am sure that you will start getting the hang of it while you start to implement more function that hooked up with the WordPress Action. I hope that this post has helped you and good luck to you!
I’ll try to keep this list current and up to date. If you know of a great resource you’d like to share or notice a broken link, please let us know.