WordPress Abilities API: permissions for AI agents

Lucchetto di ottone chiuso su un cancello bianco con la vernice scrostata

When you connect an AI agent (an AI that takes actions instead of only answering) to a WordPress site, it acts with the permissions of a user account on that site. That is where the real risk sits: a badly declared permission does more damage than any wrong answer the AI gives. The WordPress Abilities API is the registry where WordPress and its plugins list what a site can do.

WordPress 7.2, scheduled for December 8, makes it harder to get wrong: permissions now have their own field, and checks run on data that has already been cleaned.

Developers exposing functions to agents should update their abilities before the release; site owners connecting an agent should give it the least access that gets the job done.

If you don’t write code, the first two sections are the ones you need. The rest is for plugin developers.

What abilities are, and where AI comes in

An ability is a single action a site offers, described so that a program can find it and use it on its own: a name, a description, what it accepts, what it returns, and who is allowed to run it. The API arrived in WordPress 6.9, as the WordPress Developer Blog introduction to the Abilities API explains.

AI enters the picture through the official MCP Adapter plugin. MCP (Model Context Protocol) is an open standard that lets an AI such as Claude or ChatGPT use outside tools. The plugin turns a site’s abilities into MCP tools, so an agent can read data or make changes on someone’s behalf.

The agent has no powers of its own: it works with the permissions of the user it signs in as. Abilities registered by plugins are not public by default; someone has to opt them in, as the WordPress source code and the MCP Adapter README show. From 7.1 on, though, WordPress makes some of its own read-only abilities public: site, user and environment information, plus reading content, users and settings from 7.2. With the MCP Adapter active, an agent can use those right away, within its user’s permissions. That’s why picking that user matters more than anything else.

Connect an AI agent without handing over the keys

Developer examples, including the WordPress Developer Blog post on the MCP Adapter, connect the agent as an administrator, because that’s convenient on a developer’s laptop. On a live site, do the opposite. The first three steps come from that post’s security section and from the Application Passwords integration guide; the last two are common sense.

  1. Give the agent its own user. Create a new user just for the agent, with the lowest role that covers the work (in WordPress, a role is a bundle of permissions: Administrator, Editor, Author and so on). Never reuse the admin account.
  2. Give it its own password. Over the internet, the agent usually signs in with an Application Password, a secondary password that works for apps but can’t be used to log in to the dashboard (OAuth, a more involved sign-in method, is an alternative if a developer has set it up). You create and revoke it from the user’s edit screen, and the list shows when each password was last used, so you can tell whether it is still in use.
  3. Prefer read-only. On a site that’s reachable from the internet, the WordPress Developer Blog recommends giving agents mostly abilities that read (data, reports, diagnostics) rather than ones that change or delete things. Plugin developers decide which is which, which leads to the next step.
  4. Know what you’re exposing. If you install a plugin that publishes abilities for agents, ask its developer which ones are public, whether they change anything, and which permission they require. A vague answer tells you something too.
  5. Revoke what you no longer use. When the agent is no longer needed, delete its password. If you don’t need the dedicated user either, delete it as well.

If someone else runs your site, you can pass this list on to them or to your host. None of it requires touching code.

WordPress Abilities API changes in 7.2

The changes are covered in the developer note “Abilities API updates in WordPress 7.2”, published on November 19, and they’re already in the development version. There are three of them.

Declared permissions: required_capabilities

The new required_capabilities argument takes a list of capabilities, WordPress’s individual permissions such as edit_posts or manage_options. WordPress checks each one with current_user_can(), and all of them must pass before the permission_callback runs. If the list covers every case, you can leave the callback out. If the list is empty, the callback is still required, and the note reminds you that __return_true belongs only on abilities that are meant to be open to everyone.

Only put in the list what every call needs. Checks that depend on the input (“can this user edit this post?”) stay in the callback. Add a capability that only some inputs need, and the ability is hidden from, and denied to, users who could have run it with other inputs.

Input is prepared before permissions are checked

The new prepare_input() method normalizes the input, validates it against the schema, and only then sanitizes it. execute() always goes through it, so both the permission check and the execution receive the prepared value: the string "123" arrives as an integer when the schema says integer.

The change that matters most for security is about failures. If sanitizing a field fails, nested fields included, the ability stops with an ability_invalid_input error (HTTP 400 from the REST API) before the permission check and the execution. In WordPress 7.1, when converting a field failed, the REST API fell back to the original input, as the 7.1 source shows; in 6.9 and 7.0 the REST API didn’t convert fields at all, so callbacks got the values exactly as sent (all strings in a GET request). The note’s example is the string "false", which is truthy in PHP: a permission_callback that trusted a boolean field could get a yes where the caller sent a no.

Filtering the list by capability

With has_required_capabilities, available in PHP (wp_get_abilities()) and in the REST API (/wp-json/wp-abilities/v1/abilities?has_required_capabilities=true), the list shows only abilities whose declared capabilities the current user has; abilities without required_capabilities always pass this filter. The filter is optional and keeps an agent from being flooded with tools it can’t use. It is not a security check: it doesn’t run any permission_callback, and a listed ability can still refuse a specific call.

Example: a read-only ability with the right permissions

The plugin below registers an ability that counts the words in a post. It uses both levels of checking: a capability every call needs (edit_posts) and one tied to the input (being able to edit that particular post). It also runs on versions before 7.2: it only adds required_capabilities when WordPress supports it, and repeats the check in the callback either way. Save it as mysite-word-count.php in wp-content/plugins/ and activate it from the Plugins screen.

php mysite-word-count.php
<?php
/**
 * Plugin Name: Example - post word count ability
 * Description: Read-only ability that counts the words of a post the user can edit.
 * Requires at least: 6.9
 */

defined( 'ABSPATH' ) || exit;

add_action( 'wp_abilities_api_categories_init', function () {
	wp_register_ability_category(
		'mysite-content',
		array(
			'label'       => __( 'Site content', 'mysite' ),
			'description' => __( 'Abilities that read the content of this site.', 'mysite' ),
		)
	);
} );

add_action( 'wp_abilities_api_init', function () {
	$args = array(
		'label'               => __( 'Count the words of a post', 'mysite' ),
		'description'         => __( 'Returns the word count of a post the current user can edit.', 'mysite' ),
		'category'            => 'mysite-content',
		'input_schema'        => array(
			'type'                 => 'object',
			'properties'           => array(
				'post_id' => array(
					'type'    => 'integer',
					'minimum' => 1,
				),
			),
			'required'             => array( 'post_id' ),
			'additionalProperties' => false,
		),
		'output_schema'       => array(
			'type'       => 'object',
			'properties' => array(
				'post_id' => array( 'type' => 'integer' ),
				'words'   => array( 'type' => 'integer' ),
			),
		),
		// Input-dependent check: this user, this post.
		// edit_posts is repeated here for WordPress versions before 7.2.
		'permission_callback' => function ( $input ): bool {
			$input = (array) $input;
			return current_user_can( 'edit_posts' )
				&& current_user_can( 'edit_post', $input['post_id'] );
		},
		'execute_callback'    => function ( $input ) {
			$input = (array) $input;
			$post  = get_post( $input['post_id'] );
			if ( ! $post ) {
				return new WP_Error( 'mysite_post_not_found', __( 'Post not found.', 'mysite' ) );
			}
			$text  = wp_strip_all_tags( strip_shortcodes( $post->post_content ) );
			$words = preg_split( '/\s+/u', trim( $text ), -1, PREG_SPLIT_NO_EMPTY );
			return array(
				'post_id' => $post->ID,
				'words'   => is_array( $words ) ? count( $words ) : 0,
			);
		},
		'meta'                => array(
			'public'       => true, // Opt in for clients (MCP, and REST from 7.1).
			'show_in_rest' => true, // REST API on 6.9 and 7.0 too.
			'annotations'  => array( 'readonly' => true ),
		),
	);

	// WordPress 7.2 or later: the capability every call needs, declared up front.
	if ( method_exists( 'WP_Ability', 'get_required_capabilities' ) ) {
		$args['required_capabilities'] = array( 'edit_posts' );
	}

	wp_register_ability( 'mysite/count-post-words', $args );
} );

What it does: public opts the ability in for clients, and readonly declares that it changes nothing, so the REST API only runs it on GET requests. Setting additionalProperties to false rejects unexpected fields. On WordPress 6.9 and 7.0, according to the MCP Adapter guide to creating abilities, public only applies to the MCP Adapter, which is why the code also sets show_in_rest. Replace the mysite prefix with your plugin’s own.

What it can break: very little, since it only reads. To roll back, deactivate the plugin. As with any new code, try it on a copy of the site first; the guide to testing WordPress 7.2 on a staging copy explains how to set one up.

Before December 8: what to check in your plugins

If you’ve already written abilities for WordPress 6.9, 7.0 or 7.1, the developer note lists what to review:

  • Type conversions. A number schema turns 10 into 10.0, direct PHP calls included. Checks like is_int() or === 10 stop working, so use an integer schema instead. The string "false" also becomes false, and "1,2" becomes an array when the schema asks for a list of integers.
  • Calling check_permissions() directly. The method doesn’t prepare the input. If you call it yourself, call prepare_input() first and stop if it returns an error. normalize_input() alone isn’t enough, because it neither validates nor sanitizes.
  • Deprecated method. The REST controller’s sanitize_input_for_ability() is deprecated and now returns its input unchanged.
  • Empty objects. An empty result declared as an object now comes out of the REST API as {} instead of []. Always state type: object or type: array in output schemas; if you have a custom transport, prepare results with wp_prepare_json_value_for_client().
  • Older versions. A plugin that must keep working on older WordPress keeps its permission_callback: leaving it out only works from 7.2 on.

The release candidate has been out since November 17, according to the WordPress 7.2 RC1 announcement: install it on your staging copy and run your abilities there.

Sources: Abilities API updates in WordPress 7.2 (Make WordPress Core); WP_Ability class, class-wp-ability.php (WordPress source); abilities registered by WordPress, abilities.php (WordPress source); abilities list in the REST API (WordPress source); introducing the Abilities API (WordPress Developer Blog); introducing the MCP Adapter, with security advice (WordPress Developer Blog); MCP Adapter README; MCP Adapter guide to creating abilities; MCP Adapter plugin; Application Passwords integration guide (Make WordPress Core); WordPress 7.2 RC1 announcement (WordPress.org).

Featured image: padlock on a gate, by Bich Tran, via Wikimedia Commons (original on Pexels), CC0.