wordpress get current user

2019-07-01 18:01发布

I have a directory inside my wordpress directory for some template application

apacheWWW/wordpress/jtpc 

In my application i want the wordpress current user id

I am able to do this in one page but in the other I get an error

This is what I am doing to get the user id:

require_once( '/../../wp-load.php' );
global $current_user;
get_currentuserinfo();
$user_id = $current_user->ID;

but I get an error on this line

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

The error says

 File does not exist: D:/programming/apacheWWW/wordpress/jtpc/ajaxHandlers/wp-admin, referrer: http://localhost/wordpress/?page_id=23

what dose he wants with the wp-admin , and why he is looking for it in the wrong directory , It's suppose to be under

   D:/programming/apacheWWW/wordpress/

It's working for one page, (I am uploading file and creating a directory for this user id)

but for the other page I send an ajax request to delete files by user request so I need to id again, but there he throws the error

标签: php wordpress
4条回答
霸刀☆藐视天下
2楼-- · 2019-07-01 18:19

This is how you can get current user:

global $current_user;
$current_user = wp_get_current_user();

After that, you can use $current_user->ID where you want.

More Info:

查看更多
我想做一个坏孩纸
3楼-- · 2019-07-01 18:25

You may want to try one of the following two lines:

require_once STYLESHEETPATH . '/wp-load.php';

or

require_once TEMPLATEPATH . '/wp-load.php';

There is a slight difference between the STYLESHEETPATH constant and the TEMPLATEPATH constant. If you are developing a child theme, STYLESHEETPATH will be the child theme path, while TEMPLATEPATH will be the parent theme path.

查看更多
成全新的幸福
4楼-- · 2019-07-01 18:38

ok got i working i put

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

at the top of the page and not inside the function

查看更多
祖国的老花朵
5楼-- · 2019-07-01 18:39

My issue is that I've set up a standalone ajax controller for my plugin's admin. In hindsight, I wish I didn't, because now I need $current_user->ID on a particular method.

using the following code gives me undefined function errors:

global $current_user;
get_currentuserinfo();

same when I use this:

$current_user = wp_get_current_user();

I've used GREP to trace the spaghetti function calls, and was able to get stable using the following chain of includes within my ajax_request method of my base controller:

include APP_ROOT.'wp-includes'.DS.'meta.php';
include APP_ROOT.'wp-includes'.DS.'plugin.php';
include APP_ROOT.'wp-includes'.DS.'user.php';
include APP_ROOT.'wp-includes'.DS.'capabilities.php';
include APP_ROOT.'wp-includes'.DS.'load.php';
include APP_ROOT.'wp-includes'.DS.'functions.php';
include APP_ROOT.'wp-includes'.DS.'pluggable.php';

(I have defined my own APP_ROOT, which is the include path to the base of the whole application.)

This eliminates my errors, but I am still getting a user id of 0, and data is NULL, which doesn't solve my problem.

Surely there is a better way to do this. Has anyone deciphered the cookie or session variables for direct access?

Any and all suggestions would be welcome. I know I'm asking a lot for wordpress to work like normal OOP/MVC code...

In addition, I have deployed a CodeIgniter front-end, and I am using WordPress solely for the back-end. I highly recommend this for ultimate flexibility if you are forced to use WordPress. Front-end hooks are a pain, and really kind of "hacky". Here's where I got the idea:

http://jidd.jimisaacs.com/post/wordigniter-wordpress-codeigniter/

So if anyone has any questions about that, I bet I could help.

Best regards, happy coding.

查看更多
登录 后发表回答