2017-07-03 30 views
0

在wordpress中,我正在編寫一個自定義php文件,如下所示。wp current_user->自定義php文件中ID返回ID = 0

require_once (ABSPATH.'/wp-includes/pluggable.php'); 
require_once(ABSPATH.'/wp-load.php'); 
global $current_user; 
echo 'ID= ' . $current_user->ID . '<br />'; 
#CUSTOM php CODE 

此代碼是不是在我functions.php文件,但在[自定義] PHP文件,我需要的信息,以幫助負載/更新一些非WP數據庫項目。

我總是在我的代碼中的回聲線上得到ID = 0。我讀過這是關於什麼時候這是運行相對於WP init的行動。但是,不知道如何解決我的自定義PHP文件。有什麼建議麼?

回答

0

請嘗試這個你錯過了功能。

對get_currentuserinfo()的調用將當前用戶的信息放入$ userdata中,可以使用成員變量進行檢索。

<?php global $current_user; 
    get_currentuserinfo(); 

    echo 'Username: ' . $current_user->user_login . "\n"; 
    echo 'User email: ' . $current_user->user_email . "\n"; 
    echo 'User level: ' . $current_user->user_level . "\n"; 
    echo 'User first name: ' . $current_user->user_firstname . "\n"; 
    echo 'User last name: ' . $current_user->user_lastname . "\n"; 
    echo 'User display name: ' . $current_user->display_name . "\n"; 
    echo 'User ID: ' . $current_user->ID . "\n"; 
?> 

漱口輸入here

+0

感謝。我只是嘗試了幾種添加主函數的變體(包括鏈接引用中的內容),但所有用戶信息都是空的。還有其他建議嗎? – tjp

+0

@tjp在WordPress中包含自定義PHP文件的路徑。 –

+0

在我的自定義php文件中,我只包括我的原始文章中列出的兩個路徑,即pluggable.php和wp-load.php。有沒有額外的路線我需要打電話? – tjp