我不断收到“未定义函数的调用。”显示错误

我是PHP编程的新手,我试图自学WordPress主题开发很有趣,并且我使用PhpStorm作为我的IDE。

我试图更好地了解WordPress的内部功能,并且遇到了一些障碍。

我有一个沙盒插件,可以用来与WordPress一起玩。

在我的“ wp-content / plugins / sandbox / sandbox.php”文件中,我正在运行基本的PHP代码以帮助我习惯与WordPress相关的语言。

此外,我还使用Composer安装了kint和Whoops,以帮助进行调试。

现在我已经解决了这个问题,这就是我正在做的事情:

代码#1

namespace MyDevPlayground\Sandbox;

add_action( 'loop_start',__NAMESPACE__ . '\process_the_string' );
function process_the_string() {

    $current_user = wp_get_current_user();

    $data_packet = array(
        'id'    => $current_user->ID,'email' => $current_user->user_email,'name'  => array(
            'first_name' => $current_user->user_firstname,'last_name'  => $current_user->user_lastname,),);

    render_user_message( $data_packet );
}

function render_user_message( array $current_user ) {

    $user_id = $current_user['id'];

    d( "Welcome {$current_user['name']['first_name']},your user id is { {$user_id} }." );

    ddd( "Welcome {$current_user['name']['first_name']},your user id is {$user_id}." );
}

当我在上面运行代码1时,一切都很好,并且kint会很好地显示值。

现在遇到的问题是我对WordPress不了解:

代码#2

namespace MyDevPlayground\Sandbox;

add_action( 'loop_start',__NAMESPACE__ . '\check_logged_in_user' );
function check_logged_in_user(){
    $current_user = wp_get_current_user();
    if ( 0 == $current_user->ID ) {
        d('Not logged in');
    } else {
        ddd('Logged in');
    }
}

check_logged_in_user();

当我运行上述代码2时,Whoops报告以下错误:

调用未定义的函数 MyDevPlaygroundSandbox \ wp_get_current_user

由于某些原因,当我运行代码#1时, wp_get_current_user()函数可以很好地加载,但不能使用代码#2。

如果可以的话,有人可以帮助我理解为什么这是外行的说法吗?

代码1和代码2有什么区别?

wp_get_current_user() 函数为何未加载到代码#2 中,但已加载到代码#1 ?

谢谢您的帮助。

dengxingyuan 回答:我不断收到“未定义函数的调用。”显示错误

使用“ add_action”命令时,不能使用函数名称来调用该操作, 您需要像这样使用call命令:

do_action("check_logged_in_user");

更多信息:https://developer.wordpress.org/reference/functions/add_action/

本文链接:https://www.f2er.com/1251965.html

大家都在问