0

I am making a plugin with a setting page in it. In its setting page I need a user registration form with only two fields: username and password. How can I save this data to the database?

Here is what I am trying to do:

public function PLUGIN_setting_page(){
    require_once PLUGIN_PATH. 'templates/admin/plugin-settings.php';
}

plugin-settins.php:

<?php
if(isset($_POST['submit'])) {
   global $wpdb;
   $table = 'wp_myp_user';
   $data = array(
      'username' => $_POST['username'],
      'password'    => $_POST['password']
   );
   $format = array(
      '%s',
      '%s'
   );
   $success=$wpdb->insert( $table, $data, $format );
   if($success){
       echo 'data has been save' ; 
    }
} 
?>
<div class="login-page">
  <div class="form" method="post" action="">
    <form class="login-form">
      <input type="text" name="ldc_username" placeholder="username" value=""/>
      <input type="password" name="ldc_password" placeholder="password" value=""/>
      <!-- <button type="submit">login</button> -->
      <?php submit_button(); ?>
    </form>
  </div>
</div>
1

There are quite of few issues with the code. I made notes to help explain the changes. References are at the bottom, make sure to read those as well.

//isset is not the correct way. It does not check to see if the post was empty - this is the correct one ($_SERVER..)
if($_SERVER['REQUEST_METHOD'] == 'POST') {
    global $wpdb;
    //Make sure this is spelled correct.
    $table = 'wp_myp_user';

    //output will check to see if it was posted correctly.
    $output = ['status' => 1];
    //You have to sanitize the fields first - for security reasons.
    $username = sanitize_text_field($_POST["ldc_username"]);
    $password = sanitize_text_field($_POST["ldc_password"]);
    $data = array(
            //Make sure this is the same name as in the database - its spelled like this correct?
        'username' => $username,
        'password' => $password
    );
    $format = array(
        '%s',
        '%s'
    );
    //I would not put it in a variable - just send it directly.
    $wpdb->insert( $table, $data, $format );
    //Here is your post check - to console if it prints 2 - your good.
    $output['status'] = 2;
    //check reference material to learn more about wp_send_json.
    wp_send_json($output);
}
//Previously you were sending it to a function?  submit_button(); ... there is no function I can see with this. So, I would leave it out. 
?>
<div class="login-page">
   //For the action - use "#" if you want the action is happen on the same page. 
    <div class="form" method="post" action="#">
        <form class="login-form">
            <input type="text" name="ldc_username" placeholder="username" value=""/>
            <input type="password" name="ldc_password" placeholder="password" value=""/>
            <button type="submit" name="submit">login</button>
        </form>
    </div>
</div>

NOTES:

https://developer.wordpress.org/reference/functions/wp_send_json/

https://developer.wordpress.org/reference/functions/sanitize_text_field/

2
  • This is not working as well when i am submitting the form it is redirecting me to https://localhost/wordpress/wp-admin/admin.php?ldc_username=user&ldc_password=1234&submit= but the plugin page url is https://localhost/wordpress/wp-admin/admin.php?page=ldc_settings – dev Dec 1 '20 at 4:02
  • I updated my answer - since you want the form to be submitted on the page page. Use action="#" – Chris Norman Dec 2 '20 at 5:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.