• Hello,

    I am going to showcase couple of pdf files on my website. Before anybody can view the file, I would like to do the follow steps:

    1. Have a view button under the file, when the user clicks the same, they will be presented with a pop up. Now all this I can achieve, using my existing setup and plugins.
    2. The pop up will have a form with the following fields : Name, Email, Phone Number, Terms Check button and NDA check button. Submit button. All required fields. This too I can achieve using contact form 7 plugin.
    3. When the user clicks submit, he will presented with an OTP, which will have been sent to his email and phone number. How to achieve that?
    4. If the OTP is correctly entered, I would like to create a user – if new, add a new entry for the user with the pdf and latest timestamp, and all other values from the form. I would like to enter these into a custom database, since the database space on wordpress installation is limited. How can I achieve the same?

    Thanks

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator bcworkz

    (@bcworkz)

    Assuming this other database is mySQL compatible, you can connect to it by instantiating a new wpdb class object. Then any of the class methods can be used to query the DB. Be sure any user data also has the WP user ID as part of the record so your queries can be related to the current user.

    WP can send email to any valid address with wp_mail(). Sending SMS messages to phones isn’t feasible with many hosting plans. You’d need to have a SMS gateway installed on the server, which is atypical of many hosting plans. It might be something your host could add for you, or if you have a VPS plan, you should be able to add it yourself.

    Thread Starter ananddevops

    (@ananddevops)

    @bcworkz thank you for the inputs. Unfortunately, I am very fresh to wordpress development and hence even when I saw the documentation, I couldn’t figure how to implement. I will need to educate myself abt the basics before I can do that.
    And yes its a MariaDB instance hence mysql.

    As for SMS, I get what you are saying.

    Moderator bcworkz

    (@bcworkz)

    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

    Thread Starter ananddevops

    (@ananddevops)

    @bcworkz Thank you so so much, for sharing just a detailed and helpful inputs. While I was reading your note in singularity, its quite overwhelming honestly. Hence I have started to breakdown the inputs and doing 1 small step at a time.

    Hope to take a lot away from the same.

    Thank you again.

    Moderator bcworkz

    (@bcworkz)

    You’re welcome. I hadn’t realized how much I had typed into that little reply textbox! I didn’t intend to overwhelm you. I’m glad you found a way to digest it all ??

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘How to save user data into custom database?’ is closed to new replies.