If you’re keen to learn anyway, you gotta start somewhere ?? Developing a real world solution is a great way to learn, provided the goal isn’t too complex and the project could be broken down into manageable, smaller elements. It’ll be a struggle with many frustrations, but you’ll learn a lot along the way.
You can contain all of your coding efforts on a custom page template. Template code can do one thing or another based on particular values in $_REQUEST
. Without any particular values, serve a normal page which has the popup (probably actually a modal) form. The form can submit back to the same page. If your template code sees a specific value in $_REQUEST
from the form, it can send out an email with the OTP and a link to the page with yet another distinct query var added to the URL.
The OTP can be contained within the URL itself so the user needn’t retype it. An easy way to create OTPs in WP is to use its nonce scheme with wp_create_nonce()
. By default nonces are valid for 24 hours. You can verify them with wp_verify_nonce()
.
The problem with the wpdb
class docs is it assumes you’re connected to the WP DB through the global $wpdb. To connect to another mySQL DB, refer to the class constructor. Connect like so:
$mydb = new wpdb( 'dbuser', 'password:)', 'dbname', 'dbhost' );
You can then use $mydb in the same manner that the examples use $wpdb.
You can create a new user via PHP code with wp_insert_user()
. Once you have the new user’s ID, you can add any other related data to the alternate DB via wpdb
class methods from the object you instantiated. For example:
$mydb->insert( 'table_name', array( 'user_id' => $user->ID, 'field' => $form_data ), array( '%d', '%s' ));
You’ll want to define WP_DEBUG
as true
in wp-config.php so the inevitable PHP error messages will be shown right in your browser. I recommend coding in very small increments at a time and testing very frequently. But first write out everything that needs to happen in plain language in the form of comments. Then you will not lose sight of the overall picture when you are focusing on one small element of the whole.
When you are testing your code very frequently, it gets a little tedious uploading the latest version to the server every time. This is in part why local development is advantageous. Saving the file from your code editor immediately makes it available to the server. This may only save you a few clicks each time, but they add up to a lot over the length of a project.
Installing WordPress on your own Computer