Using WordPress Users In Non WordPress Applications

This is my first post under WordPress category. This tutorial will tell you how to interact with WordPress database to use its users information i.e. checking whether a user exists in your WordPress site or not, if exists then do whatever you want to do with that user.

In other words it’s like using WordPress user information from database and using in another non WordPress application. Image given below will explain you what we will do in this tutorial. Remembering my childhood day’s I made this image with mspaint :P.

 I am implementing this tutorial in my local server; you can do the same in your live projects. So I have WordPress 3.5 (Latest till date) installed in my localhost. WAMP server is installed in C drive so I installed my WordPress in C drive too. So my working folder/directory is c:\wamp\www\wordpress. For my custom login I created a folder with name “my-custom-login ” which will contain all my necessary files.

I have simply created “index.html” and “login.php” scripts inside “my-custom-login” folder to do my task of checking if entered username and passwords are correct or not.

Code for index.html:

<html>
<head>
<title>My Custom Login Form</title>
</head>
<body>
<form action="login.php" method="post">
Username: <input type="text" name="checkuser"><br />
Password:  <input type="password" name="checkpasswd"><br />
<input type="submit" value=" LOGIN ">
</form>
</body>
</html>

WordPress 3.5 encourages use of wp_authenticate() function and we have used the same. For more detail on wp_authenticate click here.

Code for login.php:

<?php require('..\wp-blog-header.php'); ?>

// Dont forget to include wp-blog-header.php
<?php

$uid = $_POST['checkuser'];
$pwd = $_POST['checkpasswd'];

/* I have not used strip_tags() and mysql_escape_string() functions here to filter username and password field. You can use them */

$user = wp_authenticate( $uid, $pwd );
if ( is_wp_error( $user ) )
echo "Login Failed!";
else
{
echo "Hello  "."<b>".$uid."</b>"."  You are logged In <br>";
echo "<b> Redirect this user whereever you want</b>";
}
?>

Note: I personally tried above code on WP 3.5 and WP3.4 and it worked well.

What if you have older version of WordPress installed in your website? Then the above code may not work properly so for older veriosns use of user_pass_ok() function would be wise. For older version of WordPress try the code for login.php given below.

Login.php for older versions of WP:

<?php require('..\wp-blog-header.php'); ?>

<?php

$uid = $_POST['checkuser'];
$pwd = $_POST['checkpasswd'];

/* I have not used strip_tags() and mysql_escape_string() functions here to filter username and password field. You can use them */

if(!user_pass_ok( $uid, $pwd ))
echo "Login Failed!";
else
{
echo "Hello  "."<b>".$uid."</b>"."  You are logged In <br>";
echo "<b> Redirect this user whereever you want</b>";
}
?>

Note: Never use user_pass_ok() with WordPress 3.5 because its use is deprecated and future versions of WP it may get eliminated so always go with wp_authenticate() function. Please share this easy and simple tutorial between WordPress lovers. I hoped it will help someone in someway :)

About sanjeev

sanjeev
Dreamer, Blogger and Thinker. I love to help people in solving their problems. You can also join me HERE

Related posts:

4 Responses so far.

  1. Sanjeev,

    Hi
    I’ve a created wordpress membership site using wishlist member plugin. But I want to have different header.php based on different logins.

    Eg: Users are: Print, Web and Free

    When Free user logs in – I want header1.php to load
    When Print user logs in – I want header2.php to load
    When Web user logs in – I want header3.php to load

    If nobody has logged in the default header.php stays.

    Please assist on how to do this.

    Regards,
    Raghav.

    1. Hi Raghav,

      In your case you would need to create separate mechanism that will map each different user to a different header file. If you have fixed number of users then you can simply do that by mapping each user with respective header file in db. Better take help of Wishlist plugin Support. Team should tell you necessary steps.

      But, tell me do you really want to change the whole header file or you just want to change the “menu” based on users.

      Regards:
      Sanjeev

      1. Sanjeev,

        Thanks for the reply. Yes I want either of the said that would solve the solution.

        I want the Free User access only the header1/top-menu1+category menu1 till he logs out..
        Same way the Web user access only the header2/top-menu2+category menu2 till he logs out..
        Other is Print user access only the header3/top-menu3+category menu3 till he logs out..

        Can this be possible. Am a newbie to PHP. Please assist on how to go forward.

        Regards,
        Raghav

        1. Well as per your requirement It would really be tricky to implement all the things. I would suggest you to have a look at Conditional Menu plugin. Look at the code and logic of this plugin. You will make your way to your solution.

          Regards:
          Sanjeev

Comments are closed.