2017-05-25 201 views
0

我试图让WordPress中当前用户如下面的代码片段当前用户:如何获得

<?php 
    include('/wp-admin/includes/user.php'); 
    $current_user = wp_get_current_user(); 
    echo 'Username: ' . $current_user->user_login; 
?> 

,但我得到了以下错误:

Fatal error: Call to undefined function wp_get_current_user() in /membri/mysite/wordpress/getUser.php on line 3

我是什么做错了,我该如何解决?

+2

添加'包括(“/可湿性粉剂管理员/包括/ pluggable.php”) ;'(不要删除用户) –

+0

你的页面位于哪里?实际上,您只需要'wp_get_current_user()'在WordPress中返回当前用户的数组。 – nyedidikeke

+0

我已经解决了函数调用,但现在我已经这个问题:调用未定义功能apply_filters()在/membri/mysite/wordpress/wp-includes/user.php上线2525 – GianLuke94

回答

0

请包括源文件 wp_get_current_user()位于

WP-包括/ pluggable.php

欲了解更多详细信息,请参阅

Wordpress Documentation

+0

为什么你会重新出任而不给予信贷回答大人物对此有何评论? –

+0

对不起。我不重新发布您的答案,我只是从wordpress文档中提到这一点 – Subi

0

在你functions.php ,你可以这样做:

function my_theme_get_user_id() { 
    global $current_user; 

    echo 'The current logged in user ID is: ' . $current_user->ID; 
} 

然后,使用操作钩子或过滤器,以使用它被需要的。

在你的主题,wp-content/themes/目录下,你可以简单地在任何文件都这样做直接的看法:

global $current_user; 
echo 'The current logged in user ID is: ' . $current_user->ID;