2017-05-25 96 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;