Thursday, November 7, 2013

Create login page in your wordpress site

You can use wordpress admin login functionality for your own website login page. It will use your wordpress table wp_users to validate credentials. Advantage here is that you can manage users via wordpress admin.

If you are using a script outside your template, you should require wp-load.php so you can use its functionality.

require_once( dirname(__FILE__) . '/wp-load.php' );

If your login page is inside your template say you have your page-login.php, then you don't need to require wp-load.php coz it's included already upon loading the page.

Once you have that in place, you can now call the functionality to validate login credential. You just simply call the function user_pass_ok() with parameters: username and password.

Please see sample usage below, you can either use it via GET or POST method, depends on how you handle your login page.

$username = @$_POST['username'];
$password = @$_POST['password'];

if (user_pass_ok($username, $password)) {
    echo 'login success';
} else {
    echo 'login failed';
}


This is just a simple post but hope this helps in a way. Please leave a comment if you like this post.

No comments:

Post a Comment