0

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?

  • Where exactly did you put these lines? – Krzysiek Dróżdż Apr 11 at 12:43
  • Indeed, I forgot this so I have added it in the question. In the plug-in's main php file. – TTT Apr 11 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 Apr 11 at 12:54
-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.

  • 2
    This shouldn't have been needed to fix the problem, that's not how PHP works, and shouldn't have made a difference. Also, shortcodes are meant to return their output, not echo them – Tom J Nowell Apr 11 at 13:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.