Abilities API


目录

The Abilities API is only available for WordPress 6.9 and above.

The WordPress Abilities API provides a standardized way to register and discover distinct units of functionality within a WordPress site. These units, called “Abilities”, represent specific actions or capabilities that components can perform, with clearly defined inputs, outputs, and permissions.

It acts as a central registry, making it easier for different parts of WordPress, third-party plugins, themes, and external systems (like AI agents) to understand and interact with the capabilities available on a specific site.

Core Concepts

Goals and Benefits

Use Cases

Registration Example

// First, register a category, or use one of the existing categories.
add_action( 'wp_abilities_api_categories_init', 'wporg_register_category' );
/**
 * Register a custom ability category.
 *
 * @return void
 */
function wporg_register_category() {
	wp_register_ability_category(
		'site-information',
		array(
			'label'       => __( 'Site Information', 'textdomain' ),
			'description' => __( 'Abilities that provide information about the WordPress site.', 'textdomain' ),
		)
	);
}

// Then, register an ability in that category.
add_action( 'wp_abilities_api_init', 'wporg_register_ability' );
/**
 * Register a custom ability to get site information.
 *
 * @return void
 */
function wporg_register_ability() {
	wp_register_ability(
		'my-plugin/site-info',
		array(
			'label'               => __( 'Site Info', 'textdomain' ),
			'description'         => __( 'Returns information about this WordPress site', 'textdomain' ),
			'category'            => 'site-information',
			'input_schema'        => array(),
			'output_schema'       => array(
				'type'       => 'object',
				'properties' => array(
					'site_name'         => array(
						'type'        => 'string',
						'description' => __( 'The name of the WordPress site', 'textdomain' ),
					),
					'site_url'          => array(
						'type'        => 'string',
						'description' => __( 'The URL of the WordPress site', 'textdomain' ),
					),
					'active_theme'      => array(
						'type'        => 'string',
						'description' => __( 'The active theme of the WordPress site', 'textdomain' ),
					),
					'active_plugins'    => array(
						'type'        => 'array',
						'items'       => array(
							'type' => 'string',
						),
						'description' => __( 'List of active plugins on the WordPress site', 'textdomain' ),
					),
					'php_version'       => array(
						'type'        => 'string',
						'description' => __( 'The PHP version of the WordPress site', 'textdomain' ),
					),
					'wordpress_version' => array(
						'type'        => 'string',
						'description' => __( 'The WordPress version of the site', 'textdomain' ),
					),
				),
			),
			'execute_callback'    => 'wporg_get_siteinfo',
			'permission_callback' => function () {
				return current_user_can( 'manage_options' );
			},
			'meta'                => array(
				'show_in_rest' => true,
			),
		)
	);
}

/**
 * Execute callback to get site information.
 *
 * @return array
 */
function wporg_get_siteinfo() {
	$active_plugins = array();
	foreach ( get_option( 'active_plugins', array() ) as $plugin_path ) {
		$plugin_data      = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin_path );
		$active_plugins[] = $plugin_data['Name'];
	}

	return array(
		'site_name'         => get_bloginfo( 'name' ),
		'site_url'          => get_bloginfo( 'url' ),
		'active_theme'      => wp_get_theme()->get( 'Name' ),
		'active_plugins'    => $active_plugins,
		'php_version'       => PHP_VERSION,
		'wordpress_version' => get_bloginfo( 'version' ),
	);
}

This creates a machine-readable capability that AI systems and automation tools can discover, understand, and execute safely within the bounds of WordPress permissions and validation rules.


版本号 #1
由 壳壳虫 创建于 2026-08-29 23:36:33 UTC
由 壳壳虫 更新于 2026-08-29 23:36:33 UTC