Categories

Saturday, December 21, 2024
#919814419350 therichposts@gmail.com
WoocommerceWoocommerce HooksWordpressWordpress Cheat CodesWordpress Hookswordpress rest apiWordpress Tricks

How to get current logged in user using WordPress Rest Api?

How to get current logged in user using Wordpress Rest Api?

Hello guys, how are you? Welcome back on blog therichpost.com. To obtain information about the current logged-in user through the WordPress REST API, please follow the below logic guys.

$user_id = ""; //<- add this

add_action( 'rest_api_init', 'add_custom_users_api');

function add_custom_users_api(){
    $GLOBALS['user_id'] = get_current_user_id(); //<- add this

    // route url: domain.com/wp-json/mmw/v1/testing
    register_rest_route( 'mmw/v1', 'testing', array(
        'methods' => 'GET',
        'callback' => 'get_custom_users_data',
    ));
}
//Customize the callback to your liking
function get_custom_users_data(){
    return $GLOBALS['user_id']; //<- add this
}

Remember, for security reasons, direct use of admin credentials in scripts or applications is not recommended. Instead, use application passwords or OAuth tokens, which can be revoked if necessary.

Guys if you will have any kind of query then feel free to comment below.

Jassa

Thanks

therichpost
the authortherichpost
Hello to all. Welcome to therichpost.com. Myself Ajay Malhotra and I am freelance full stack developer. I love coding. I know WordPress, Core php, Angularjs, Angular 19, MedusaJs, Next.js, Bootstrap 5, Nodejs, Laravel, Codeigniter, Shopify, Squarespace, jQuery, Google Map Api, Vuejs, Reactjs, Big commerce etc.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.