WordPress current_user_can(): Capability Checks in Admin Pages, AJAX, and REST Routes

current_user_can(): capability checks in admin pages, AJAX and REST

Every decision “may this user do this?” in WordPress belongs to the capability system. current_user_can() checks the logged-in user against a capability, and a missing check is the most common cause of privilege escalation bugs in plugins: a settings page, an AJAX handler or a REST route that any logged-in user, or even a guest, can call directly.

Problem: A plugin has a settings page, an AJAX handler and a REST endpoint that should be used only by administrators, but a subscriber can call the AJAX action and the endpoint with a crafted request.

Solution: Check the capability at every entry point: the capability argument of add_menu_page() plus a check in the page callback, current_user_can() at the top of every AJAX handler, and the permission_callback of every REST route. For a single post, use meta capabilities such as edit_post with the post ID.

Admin pages

<?php
add_action( 'admin_menu', function () {
    add_menu_page( 'My Plugin', 'My Plugin', 'manage_options', 'ha-settings', 'ha_render_settings' );
} );

function ha_render_settings() {
    if ( ! current_user_can( 'manage_options' ) ) {
        wp_die( esc_html__( 'You are not allowed to access this page.', 'textdomain' ) );
    }
    // the form...
}

The capability in add_menu_page() hides the menu and blocks the page URL. The check in the callback costs nothing and protects you when the same callback is reused somewhere else. Code that saves the settings (an admin_post_ or admin_init handler) needs its own check: it does not run inside the page.

AJAX handlers

Every logged-in user, including subscribers, can call any wp_ajax_ action. The nonce does not change that, because a user can get the nonce from a page they are allowed to see:

<?php
add_action( 'wp_ajax_ha_save_settings', 'ha_ajax_save_settings' );

function ha_ajax_save_settings() {
    check_ajax_referer( 'ha_save_settings', 'nonce' ); // against requests from other sites

    if ( ! current_user_can( 'manage_options' ) ) {    // against users without the right
        wp_send_json_error( array( 'message' => 'Permission denied.' ), 403 );
    }

    update_option( 'ha_settings', array_map( 'sanitize_text_field', wp_unslash( (array) $_POST['settings'] ) ) );
    wp_send_json_success();
}

A small AJAX example from start to finish is in handling AJAX requests with wp_ajax.

REST routes

<?php
add_action( 'rest_api_init', function () {
    register_rest_route( 'ha/v1', '/settings', array(
        'methods'             => 'POST',
        'callback'            => 'ha_rest_save_settings',
        'permission_callback' => function () {
            return current_user_can( 'manage_options' );
        },
    ) );
} );

For logged-in users from the browser, the request must also send the X-WP-Nonce header (a nonce for wp_rest), otherwise WordPress treats it as a guest request and the check returns false.

Meta capabilities for one object

edit_posts means “can edit posts in general”. Whether a user may edit this particular post depends on the author, the status and the post type, and edit_post with an ID checks exactly that:

<?php
if ( current_user_can( 'edit_post', $post_id ) ) {
    // show the edit link for this post
}

if ( current_user_can( 'delete_post', $post_id ) ) {
    // show the delete button
}

// Another user than the current one
$user = get_userdata( 5 );
if ( $user && $user->has_cap( 'manage_options' ) ) {
    // user 5 is an administrator
}

The default roles: read = every logged-in user, edit_posts = contributor and above, publish_posts and upload_files = author and above, edit_others_posts = editor and above, manage_options and install_plugins = administrator.

NOTE: Check capabilities, not roles: current_user_can( 'administrator' ) works by accident and breaks with custom roles and in Multisite. For guests current_user_can() always returns false, so no separate is_user_logged_in() check is needed. And never use '__return_true' as the permission_callback of a route that changes data.