最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - Call to undefined function get_userdata() in plugin

matteradmin8PV0评论

After refactoring/restructuring files in my plug-in, I now get the error

Uncaught Error: Call to undefined function get_userdata()

after code

$current_user_id = get_current_user_id();
$current_user_meta = get_userdata($current_user_id);

in mydomain.local/wp-content/plugins/my-project/my-project.php I also tried as it was before, in another .php file that was required by my-project.php

These lines are and were appearing quite early in my plug-in code ... am I missing some dependency before running them?

After refactoring/restructuring files in my plug-in, I now get the error

Uncaught Error: Call to undefined function get_userdata()

after code

$current_user_id = get_current_user_id();
$current_user_meta = get_userdata($current_user_id);

in mydomain.local/wp-content/plugins/my-project/my-project.php I also tried as it was before, in another .php file that was required by my-project.php

These lines are and were appearing quite early in my plug-in code ... am I missing some dependency before running them?

Share Improve this question edited Apr 11, 2019 at 13:18 nmr 4,5672 gold badges17 silver badges25 bronze badges asked Apr 11, 2019 at 12:42 TTTTTT 3291 gold badge4 silver badges17 bronze badges 3
  • Where exactly did you put these lines? – Krzysiek Dróżdż Commented Apr 11, 2019 at 12:43
  • Indeed, I forgot this so I have added it in the question. In the plug-in's main php file. – TTT Commented Apr 11, 2019 at 12:48
  • I think I found the reason. It may be that the lines are not correctly wrapped anymore ... Looking to check and correct it. – TTT Commented Apr 11, 2019 at 12:54
Add a comment  | 

1 Answer 1

Reset to default -1

Solution found: the reason was that the code including those lines were included in plug-in's main file and were wrapped in the shortcode launch anymore.

I had to put things in another file,like for example

function load_shortcode( $atts ){
    require_once(getPluginPath().('my-project.start.php'));
}
add_shortcode( 'my-project', 'load_shortcode' );

and lines had to be in my-project.start.php


getPluginPath() is NOT a native WordPress function, don't look for it, the only thing to know here is that it generates a path string.

Post a comment

comment list (0)

  1. No comments so far