get user id in a plugin without a function

I have an admin page, in the callback of this admin page, i need to query into current user’s posts. So let’s assume this is my function to add menu page:

add_menu_page( ‘My billings ‘, ‘Billings’, ‘manage_options’, ‘billings’,
‘billings_html’, ‘dashicons-analytics’ );

And this is the callback function :

function billings_html() {
$user_id = get_current_user_id();

$barbershops = new WP_Query( array(
‘author’ => $user_id,
‘post_type’ => ‘barbershop’
) );

print_r( $barbershops );

}

I need to mention that my code is in plugin. I can’t wrap my code to get the ID in a function and run it with an action. As you know, this is the error i get:

Uncaught Error: Call to undefined function wp_get_current_user() …

I don’t know how to get the user id in this plugin.

Read more here:: get user id in a plugin without a function

Leave a Reply

Your email address will not be published. Required fields are marked *