• Resolved iamsiraskalot

    (@iamsiraskalot)


    I am new to PHP programming and I am trying to teach myself WordPress theme development for fun and I am using PhpStorm as my IDE.

    I am trying to better understand the inner-workings of WordPress and I am running into a roadblock on something.

    I have a sandbox plugin that I created to use to play around with WordPress.

    In my “wp-content/plugins/sandbox/sandbox.php” file, I am just running basic PHP code to help me get used to the language as it relates to WordPress.

    Also, I installed both Kint and Whoops using Composer to help with debugging.

    Now that I got that out of the way, here is what I am doing:

    Code #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}." );
    }

    When I run Code #1 above everything is fine and Kint displays the values just fine.

    Now for the problem I am having that I don’t understand about WordPress:

    Code #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();

    When I run Code #2 above, Whoops reports the following error:

    Call to undefined function MyDevPlaygroundSandbox\wp_get_current_user()

    For some reason when I run Code #1, the wp_get_current_user() function loads just fine, but not with Code #2.

    Can someone help me understand why this is in laymen’s terms if possible?

    Thank you for your help.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Joy

    (@joyously)

    I think it’s because the second one is invoking the check_logged_in_user function directly, without the rest of WP.
    But I never use namespaces (I think it just complicates things), and I have never heard of the tools you mentioned, so I don’t know what you are actually doing.

    Thread Starter iamsiraskalot

    (@iamsiraskalot)

    Yes, you’re exactly right!

    Sorry, I completely forgot to post the answer after I found it.

    I needed to wrap the call to the function in a “do_action”.

    Oh you’ve never heard of Kint or Whoops?

    What do you use for debugging PHP code for WP development purposes?

    I’m always curious to know the workflow of others.

    Thanks

    Joy

    (@joyously)

    I have used the error_log function to output variables, if needed, but mostly I build up from small functions that are easy to test independently, and do research in the Code Reference for integration into WP. No special tools needed.

    For themes especially, there is not as much to do as for plugins. We are in a transition period for themes. Old style is entirely PHP, with CSS (and JS as needed). The future will be blocks, and CSS and JSON, with very little else.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘A “Call to undefined function..” error keeps displaying’ is closed to new replies.