AJAX post to WordPress and get current user ID

1.2k Views Asked by At

I have a custom form in WordPress. A JavaScript (angularjs) posts the form to the admin area when the admin is logged in. How can this script call get current user id? The form is accessible only when admin user logs in.

It is always returning 0.

1

There are 1 best solutions below

0
George Denning On

You should be using the following action, which will allow calls to the function current_user_id.

add_action( 'wp_ajax_nopriv_current_user_id', 'prefix_ajax_current_user_id' );

You could also just pass the user ID to the script using the wp_localize_script function from your themes enqueue scripts action.