2
votes

I am currently trying to write a couple of pages into my website that are not part of the wordpress site but I would like to be able to use the wordpress users. I have this working using the following code

require_once("../../../wp-load.php");
$current_user = wp_get_current_user();

Now I am able to use the $current_user variable for everything I need. However because I am includeing the wp-load.php file there is a lot of overhead that I really don't need.

My question is how can I get the current wordpress user without including wp-load?

I don't mind having to include a few extra files myself but I really don't need or want the entire wp enviroment to be set-up each time this page is called just so I can get the user.

1

1 Answers

0
votes

What are you doing in those pages? I'm asking because, if you want to send some POST via ajax, you should check this.

If you don't use ajax, but you just need some pages where you can acces Wordpress functions, well somehow you must include wp-load because this file is loading Wordpress.

I suggest creating a file called page-custom-name.php in your theme folder, and publish an empty page with the exact title "Custom-name". Now when you'll be visiting www.yoursite/custom-name you will see that page, and you can get the current user info, or access other Wordpress functions.

Later edit:

This idea a partial solution: How about using hooks?

In your functions.php put these lines:

add_action('wp_login', 'aboutuser');

function aboutuser($username, $user)
{
  $userObject = $user;

  //find a solution to send this data to your url of your application
}

Basically, when users are logging in Wordpress, the aboutuser() function is executed, and it's get 2 params(in Wordpress 3.3; in earlier versions it gets only the username).

Now, when users are logging in we have acces to the wp user object. Maybe there is a way pass this data(a POST request) to your application and store it in a session.

I don't sure if it's possible though.