-1

Please I need help with this.

I wrote a basic code to create a login form that will validate, retrieve data and display on my dashboard but I have the following challenges:

  1. It always displays "Invalid login details" whether I enter the right details or not.
  2. I don't know how to retrieve the data and display the data of the logged in user in my dashboard
  3. I wish to use sessions like I would in PHP but I don't know how to do that

Kindly note that I created a new table in the database and I'm not using the default users table in WordPress.

Please find my code below, I haven't even created the logout:

function login_form ()
{

    $loginform = '';

    $loginform .= '<form method="post" action="example.com/dashboard/">';
    $loginform .= '<br />';
    $loginform .= '<br />';

    $loginform .= '<input type="text" size="40" name="email" placeholder="Email">';
    $loginform .= '<br />';
    $loginform .= '<br />';

    $loginform .= '<input type="password" size="40" name="passcode" placeholder="Password">';
    $loginform .= '<br />';
    $loginform .= '<br />';

    $loginform .= '<input type="submit" name="login_submit" value="Log In">';

    $loginform .= '</form>';
    $loginform .= '<br />';


    return $loginform;

}
add_shortcode('my_login','login_form');



function login_form_mail()
{
    if (array_key_exists('login_submit', $_POST))
    {

        global $wpdb;

        $loginusername = $_POST['email'];
        $loginpassword = $_POST['passcode']; 

        $retrieveUsername = $wpdb->get_var(" SELECT email FROM ".$wpdb->prefix."my_dataform WHERE email = '$loginusername'");

        $retrievePassword = $wpdb->get_var(" SELECT passcode FROM ".$wpdb->prefix."my_dataform WHERE passcode = '$loginpassword'");

       if ($loginusername == $retrieveUsername && $loginpassword == $retrievePassword)
       {
            echo 'Welcome ".$loginusername."';
       }
       else
       {
            echo 'Invalid login details';
       }
    }


} 
add_action('wp_head','login_form_mail');

closed as off-topic by Nathan Johnson, Andy Macaulay-Brook, Rick Hellewell, Max Yudin, Christine Cooper Mar 27 at 14:11

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third party plugins and themes are off topic, they are better asked about at their developers' support routes." – Nathan Johnson, Andy Macaulay-Brook, Rick Hellewell, Max Yudin, Christine Cooper
If this question can be reworded to fit the rules in the help center, please edit the question.

1

Have you started here in the Codex (always a good place to go): https://codex.wordpress.org/Customizing_the_Login_Form . Lots of help there (and in links therein) on how to customize the login form.

And, lots of tutorials found via the googles.

Your code is assuming a lot of things that aren't true. Forms in WP are different than forms in PHP/HTML. The query to get login authentication is different than you are trying, if you are accessing the WP user table. And your query into your own database is also in error - your 'and' condition is not properly formatted.

Rather than providing all of the answers - and reinventing the wheel because of all the googles on this subject - I recommend you start with the Codex link above, and then look at all of the tutorials on this subject.

It's not clear whey you would want to have a separate user table, either - what is it about the WP user table that doesn't meet your needs? You can add user roles to the WP user table to customize access levels.

So, more (much more) detail is needed. This question is a bit broad in scope.

  • Thanks Rick. Your answer is assuming that I'm using the users table. "Kindly note that I created a new table in the database and I'm not using the default users table in WordPress" I'm trying to do this as a simple plugin. – John Mike Feb 28 at 2:24
  • My next-to-last paragraph in my answer notes your use of a new table. And the 2nd paragraph noted that your query conditions are incorrect. Try adding brackets around each condition to ensure that both conditions are being met. – Rick Hellewell Feb 28 at 18:36
  • Learn to code or google for a piece of code or a site where you can hire somebody. Not the right place to hire. Read the rules before you post. – Max Yudin Mar 26 at 20:15

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