不用手动适配,Abilities API 让 AI 一键识别 WordPress 站点所有能力!
经常在 WordPress 对接 AI 的同学都知道,对接容易,适配难,接口无统一标准,耗时又费力。怎么办呢?
什么是 Abilities API?
为此,WordPress 6.9 推出的 Abilities API,彻底终结了这种困扰,作为全新的底层基础系统,它使插件、主题和 WordPress 核心能够以标准化的、机器可读的格式注册并公开其功能,彻底摆脱手动适配的麻烦。
该 API 创建了一个统一的功能注册表,让这些能力能在 PHP、REST API 以及在和 AI 集成中,以一致的方式被发现、验证和执行,这也是它能让 AI 一键识别 WordPress 站点所有能力的核心原因。
其中,Ability(能力)是一个独立的功能单元,每个 Ability(能力)都有明确的输入、输出、权限校验规则和执行逻辑,通过 Abilities API 注册后,开发者可以:
- 利用标准化接口创建可发现的功能,让 AI/其他工具能快速识别,无需手动适配;
- 统一定义权限校验规则和执行回调函数,无需重复开发适配代码;
- 将能力按逻辑分类,提升管理和发现效率,方便 AI 快速检索;
- 自动完成输入和输出的校验,减少开发bug,降低调试成本
- 让注册的能力自动通过 REST API 端点暴露,实现多端调用,无需额外开发接口。
与其将功能隐藏在孤立的函数或自定义 AJAX 处理程序中,不如将功能注册到中央注册表中,使其可以通过多个接口访问。
相较于以往将功能隐藏在孤立的函数或自定义 AJAX 处理程序中,通过 Abilities API 注册的能力会纳入中央注册表,可通过多种接口统一访问。这就相当于给 WordPress 所有功能贴了“AI 可识别标签”,AI 无需再猜测、再适配,只要查询这个注册表,就能一键摸清 WordPress 站点的所有能力。
Abilities API 的三大核心组件
Abilities API 主要由 PHP API、REST API 端和 Hooks 三部分构成,三者协同工作,实现能力的注册、管理、调用和扩展,是整个系统的底层支撑,也是 AI 能顺畅识别、调用 WordPress 能力的基础。
1. PHP API
一组用于注册、管理和执行能力的函数:
能力管理:
wp_register_ability()– 注册一项新能力wp_unregister_ability()– 取消注册一项能力wp_has_ability()– 检查某项能力是否已注册wp_get_ability()– 检索已注册的能力wp_get_abilities()– 检索所有已注册的能力
能力类别管理:
wp_register_ability_category()– 注册能力类别wp_unregister_ability_category()– 取消注册能力类别wp_has_ability_category()– 检查能力类别是否已注册wp_get_ability_category()– 获取已注册的能力类别wp_get_ability_categories()– 获取所有已注册的能力类别
2. REST API 端
Abilities API 会在 wp-abilities/v1 命名空间下,自动为已注册的能力暴露标准化 REST API 端点,支持远程对能力进行查询、执行,无需开发者手动编写接口代码——这意味着 AI 工具可通过远程调用,一键识别、执行 WordPress 站点的所有能力,彻底省去手动适配接口的步骤。
GET /wp-abilities/v1/categories– 列出所有能力类别GET /wp-abilities/v1/categories/{slug}– 获得单一能力类别GET /wp-abilities/v1/abilities– 列出所有能力GET /wp-abilities/v1/abilities/{name}– 获得一项能力GET|POST|DELETE /wp-abilities/v1/abilities/{name}/run– 执行一项能力
3. 钩子(Hooks)
新增与 Abilities API 集成的 Hooks:
动作钩子(Actions):
wp_abilities_api_categories_init– 当能力类别注册表初始化时触发(在此处注册类别)wp_abilities_api_init– 当技能注册表初始化时触发(在此处注册技能)wp_before_execute_ability– 在技能执行前触发wp_after_execute_ability– 在技能执行完毕后触发
过滤器钩子(Filters):
wp_register_ability_category_args– 注册前筛选能力类别参数wp_register_ability_args– 注册前筛选能力参数
注册能力(Ability)
能力必须在 wp_abilities_api_init 动作钩子中注册,若在外部注册会触发 _doing_it_wrong() 提示,且注册失败;能力分类需在 wp_abilities_api_categories_init 钩子中注册,且需先于能力注册,这是保证 AI 能正常识别能力的关键前提。
下面代码是注册能力类别和能力一个简单的完整示例:
<?php
add_action( 'wp_abilities_api_categories_init', 'my_plugin_register_ability_categories' );
/**
* Register ability categories.
*/
function my_plugin_register_ability_categories() {
wp_register_ability_category(
'content-management',
array(
'label' => __( 'Content Management', 'my-plugin' ),
'description' => __( 'Abilities for managing and organizing content.', 'my-plugin' ),
)
);
}
add_action( 'wp_abilities_api_init', 'my_plugin_register_abilities' );
/**
* Register abilities.
*/
function my_plugin_register_abilities() {
wp_register_ability(
'my-plugin/get-post-count',
array(
'label' => __( 'Get Post Count', 'my-plugin' ),
'description' => __( 'Retrieves the total number of published posts.', 'my-plugin' ),
'category' => 'content-management',
'input_schema' => array(
'type' => 'string',
'description' => __( 'The post type to count.', 'my-plugin' ),
'default' => 'post',
),
'output_schema' => array(
'type' => 'integer',
'description' => __( 'The number of published posts.', 'my-plugin' ),
),
'execute_callback' => 'my_plugin_get_post_count',
'permission_callback' => function() {
return current_user_can( 'read' );
},
)
);
}
/**
* Execute callback for get-post-count ability.
*/
function my_plugin_get_post_count( $input ) {
$post_type = $input ?? 'post';
$count = wp_count_posts( $post_type );
return (int) $count->publish;
}
下面是一个包含更高级的输入输出模式、输入验证和错误处理更复杂的示例:
<?php
add_action( 'wp_abilities_api_init', 'my_plugin_register_text_analysis_ability' );
/**
* Register a text analysis ability.
*/
function my_plugin_register_text_analysis_ability() {
wp_register_ability(
'my-plugin/analyze-text',
array(
'label' => __( 'Analyze Text', 'my-plugin' ),
'description' => __( 'Performs sentiment analysis on provided text.', 'my-plugin' ),
'category' => 'text-processing',
'input_schema' => array(
'type' => 'object',
'properties' => array(
'text' => array(
'type' => 'string',
'description' => __( 'The text to analyze.', 'my-plugin' ),
'minLength' => 1,
'maxLength' => 5000,
),
'options' => array(
'type' => 'object',
'properties' => array(
'include_keywords' => array(
'type' => 'boolean',
'description' => __( 'Whether to extract keywords.', 'my-plugin' ),
'default' => false,
),
),
),
),
'required' => array( 'text' ),
),
'output_schema' => array(
'type' => 'object',
'properties' => array(
'sentiment' => array(
'type' => 'string',
'enum' => array( 'positive', 'neutral', 'negative' ),
'description' => __( 'The detected sentiment.', 'my-plugin' ),
),
'confidence' => array(
'type' => 'number',
'minimum' => 0,
'maximum' => 1,
'description' => __( 'Confidence score for the sentiment.', 'my-plugin' ),
),
'keywords' => array(
'type' => 'array',
'items' => array(
'type' => 'string',
),
'description' => __( 'Extracted keywords (if requested).', 'my-plugin' ),
),
),
),
'execute_callback' => 'my_plugin_analyze_text',
'permission_callback' => function() {
return current_user_can( 'edit_posts' );
},
)
);
}
/**
* Execute callback for analyze-text ability.
*
* @param $input
* @return array
*/
function my_plugin_analyze_text( $input ) {
$text = $input['text'];
$include_keywords = $input['options']['include_keywords'] ?? false;
// Perform analysis (simplified example)
$sentiment = 'neutral';
$confidence = 0.75;
$result = array(
'sentiment' => $sentiment,
'confidence' => $confidence,
);
if ( $include_keywords ) {
$result['keywords'] = array( 'example', 'keyword' );
}
return $result;
}
此外 Abilities API 使用 JSON Schema 进行输入和输出验证,WordPress 实现了一个基于 JSON Schema 版本 4 子集的验证器。
执行能力(Ability)
要执行一项能力,首先需要获取该能力,然后才能执行。这通常是在当前 init 动作期间或之后的任何动作中执行。
add_action( 'init', 'my_plugin_execute_ability' );
/**
* Function which fetches and executes an ability.
*
* @return void
*/
function my_plugin_execute_ability() {
$get_post_count_ability = wp_get_ability( 'my-plugin/get-post-count' );
$result = $get_post_count_ability->execute();
// do something with $result
}
使用 REST API
开发者还可以启用能力以支持默认的 REST API,这可以通过在注册能力时设置 meta.show_in_rest 参数为 true 来实现。
wp_register_ability(
'my-plugin/get-post-count',
array(
'label' => __( 'Get Post Count', 'my-plugin' ),
'description' => __( 'Retrieves the total number of published posts.', 'my-plugin' ),
'category' => 'content-management',
'input_schema' => array(
'type' => 'string',
'description' => __( 'The post type to count.', 'my-plugin' ),
'default' => 'post',
),
'output_schema' => array(
'type' => 'integer',
'description' => __( 'The number of published posts.', 'my-plugin' ),
),
'execute_callback' => 'my_plugin_get_post_count',
'permission_callback' => function() {
return current_user_can( 'read' );
},
'meta' => array(
'show_in_rest' => true,
)
)
);
访问所有 Abilities REST API 需要经过验证身份,Abilities API 支持所有 WordPress REST API 身份验证方法:
- Cookie 认证(同源请求)
- 应用程序密码(建议用于外部访问)
- 自定义身份验证插件
启用后,即可通过 REST API 端点列出、获取和执行能力:
列出所有能力:
curl -u 'USERNAME:APPLICATION_PASSWORD' \
https://example.com/wp-json/wp-abilities/v1/abilities
获得一项能力:
curl -u 'USERNAME:APPLICATION_PASSWORD' \
https://example.com/wp-json/wp-abilities/v1/abilities/my-plugin/get-post-count
执行一项能力:
curl -u 'USERNAME:APPLICATION_PASSWORD' \
-X POST https://example.com/wp-json/wp-abilities/v1/abilities/my-plugin/get-post-count/run \
-H "Content-Type: application/json" \
-d '{"input": {"post_type": "page"}}'
API 会自动根据能力的输入模式验证输入,通过能力的权限回调检查权限,执行能力,根据能力的输出模式验证输出,并将结果以 JSON 格式返回。
能力查询和错误处理
你可以检查某个能力是否存在,并通过编程方式检索它:
<?php
// Check if an ability is registered
if ( wp_has_ability( 'my-plugin/get-post-count' ) ) {
// Get the ability object
$ability = wp_get_ability( 'my-plugin/get-post-count' );
// Access ability properties
echo $ability->get_label();
echo $ability->get_description();
}
// Get all registered abilities
$all_abilities = wp_get_abilities();
foreach ( $all_abilities as $ability ) {
echo $ability->get_name();
}
能力错误处理使用 WordPress 标准方式,返回 WP_Error对象:
<?php
function my_plugin_delete_post( $input ) {
$post_id = $input['post_id'];
if ( ! get_post( $post_id ) ) {
return new WP_Error(
'post_not_found',
__( 'The specified post does not exist.', 'my-plugin' ),
);
}
$result = wp_delete_post( $post_id, true );
if ( ! $result ) {
return new WP_Error(
'deletion_failed',
__( 'Failed to delete the post.', 'my-plugin' ),
);
}
return array(
'success' => true,
'post_id' => $post_id,
);
}
最后总结两句
说到底,Abilities API 作为 WordPress 6.9 为 AI 时代打造的底层基础系统,核心价值就是标准化——让 WordPress 所有功能以机器可读的格式被注册、发现、调用,彻底解决了 AI 与 WordPress 对接时可以快速适配。
对于 AI 开发而言,这套 API 让 AI 能一键识别 WordPress 站点的所有能力,无需再研究不同插件/核心的调用逻辑,直接通过统一标准调用;对于 WordPress 开发者而言,它也简化了功能开发、多端适配和插件协同的流程,提升开发效率的同时,让 WordPress 生态能更好地融入 AI 时代,这也是官方推出这套 API 的核心初衷。
对于广大 WordPress 开发者来说,应该尽早掌握这套 API 的注册、调用和开发规范,能让自己的插件/主题提前适配 AI 时代,抓住全新的开发机遇。