簡體   English   中英

Wordpress:致命錯誤:調用未定義的函數wp_get_current_user()

[英]Wordpress: Fatal error: Call to undefined function wp_get_current_user()

因此,我在下面的法典頁面https://codex.wordpress.org/Function_Reference/wp_get_current_user中查看示例,以確定是否從我的插件中登錄了用戶,並獲得了上面提到的內容錯誤信息:

$current_user = wp_get_current_user();
if ( 0 == $current_user->ID ) {
    // Not logged in.
} else {
    // Logged in.
}

我在這里想念什么?

另外:我希望實現的是,無論用戶是否登錄,都允許我代碼中的jQuery運行。 但是,如果用戶已注銷,則JQuery無法正常工作:

 function getregions_scripts() { global $post; $tempHost = $_SERVER['HTTP_HOST']; if ( in_array( $post->post_name, array( 'homepage', 'home-tests') ) && in_array( $tempHost, array( '192.0.0.0', '192.0.0.1' ) ) ){ wp_enqueue_script( 'getregions-script', plugin_dir_url(__FILE__) . "assets/getregions.js", array('jquery'), '1.0', true ); } wp_localize_script( 'getregions-script', // this needs to match the name of our enqueued script 'gymRegions', // the name of the object array('ajaxurl' => admin_url('admin-ajax.php')) // the property/value ); } add_action('init','checkUserExists'); function checkUserExists(){ $current_user = wp_get_current_user(); if ( 0 == $current_user->ID ) { add_action( 'wp_enqueue_scripts', 'getregions_scripts' ); add_action( 'wp_ajax_showcountries', 'showcountries_callback' ); add_action( 'wp_ajax_no_priv_showcountries', 'showcountries_callback' ); add_action( 'wp_ajax_showcountries_frontend', 'showcountries_frontend' ); add_action( 'wp_ajax_no_priv_showcountries_frontend', 'showcountries_frontend' ); } else { add_action( 'wp_enqueue_scripts', 'getregions_scripts' ); add_action( 'wp_ajax_showcountries', 'showcountries_callback' ); add_action( 'wp_ajax_no_priv_showcountries', 'showcountries_callback' ); add_action( 'wp_ajax_showcountries_frontend', 'showcountries_frontend' ); add_action( 'wp_ajax_no_priv_showcountries_frontend', 'showcountries_frontend' ); } } function showcountries_callback() { } 

您必須將代碼包裝在init掛鈎中,因為包含該功能的文件稍后會被wordpress包含。

add_action('init','your_function');
function your_function(){
  $current_user = wp_get_current_user();
  // your code
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM