WordPress 6.9 is shipped with numerous crowd pleasing choices. Among the ones is a brand spanking new API known as Talents API.
The Skills API provides a standardized method for WordPress core, plugins, and problems to stipulate their options in a construction every other people and machines can be informed.
In this publish, we’ll uncover what the Skills API is, why it problems, and the way you’ll use it in practice with some code examples.
Without further ado, let’s get started.
What’s Skills API?
The Skills API is a brand spanking new serve as in WordPress that acts like a dictionary of everything a internet web site can do. Forward of this, there was once no simple or consistent method for plugins or external tools to discover a internet web site’s available choices. Capacity was once steadily scattered all through hooks, REST API endpoints, and moderately numerous pieces of custom designed code.
With the Skills API, automation tools, AI assistants, and other plugins can additional merely know the way to have interaction with a WordPress internet web site. Apparatus like AI agents, Zapier, or n8n can simply ask WordPress, “What can you do?” and acquire a structured tick list of abilities.
This API moreover makes cross-plugin collaboration so much cleaner. Plugins can call every other’s abilities directly as a substitute of relying on hidden hooks or fragile workarounds.
Getting Started
To use the Skills API, the first step is to enroll a brand spanking new method. This is most often accomplished inside your plugin or theme. An “Ability” must contain:
- A singular name this is composed of most straightforward lowercase alphanumeric characters, dashes, and forward slashes e.g.
hongkiatcom/create-invoice - A human-readable description and label.
- A defined output and input schema.
- A permission check out.
Proper right here is a simple example of how we join an ability that analyzes a internet web site and returns some metrics.
In this example, we call it hongkiatcom/site-analytics-summary. It doesn’t require any inputs and returns an object containing 3 metrics: visits, signups, and product sales. The facility can most straightforward be carried out thru shoppers with the manage_options capability.
add_action( 'wp_abilities_api_init', function () {
if ( ! function_exists( 'wp_register_ability' ) ) {
return;
}
wp_register_ability(
'hongkiatcom/site-analytics-summary',
[
'label' => __( 'Get Site Analytics Summary', 'myplugin' ),
'description' => __( 'Returns a simple overview of site performance.', 'myplugin' ),
'input_schema' => [
'type' => 'object',
'properties' => [],
],
'output_schema' => [
'type' => 'object',
'properties' => [
'visits' => [ 'type' => 'integer' ],
'signups' => [ 'type' => 'integer' ],
'product sales' => [ 'type' => 'integer' ],
],
],
'permission_callback' => function () {
return current_user_can( 'manage_options' );
},
'execute_callback' => function () {
return [
'visits' => 1473,
'signups' => 32,
'sales' => 5,
];
},
]
);
});
Right here’s each different example of registering an ability that processes an order. This implies takes in purchaser ID, product SKUs, and a value token as input, and returns the order ID, status, and a confirmation message as output.
add_action( 'init', function() {
if ( ! function_exists( 'wp_register_ability' ) ) {
return;
}
wp_register_ability( 'hongkiatcom/process-order', [
'description' => 'Handles payment, creates an order record, and sends a confirmation email.',
'execute_callback' => function () {
// Implementation of order processing logic goes here...
},
'input_schema' => [
'type' => 'object',
'properties' => [
'customer_id' => [
'type' => 'integer',
'description' => 'The ID of the customer placing the order.',
],
'product_skus' => [
'type' => 'array',
'description' => 'An array of product SKUs (strings) to be included in the order.',
'items' => [ 'type' => 'string' ],
],
'payment_token' => [
'type' => 'string',
'description' => 'A secure, single-use token from the payment gateway.',
],
],
'required' => [ 'customer_id', 'product_skus', 'payment_token' ],
],
'output_schema' => [
'type' => 'object',
'properties' => [
'order_id' => [
'type' => 'integer',
'description' => 'The ID of the newly created order post.',
],
'order_status' => [
'type' => 'string',
'description' => 'The resulting status of the order (e.g., "processing", "pending").',
],
'message' => [
'type' => 'string',
'description' => 'A confirmation message.',
],
],
],
] );
} );
Executing Skills
Registering an ability doesn’t do so much on its own. We moreover need to execute it and get the result.
In PHP
In PHP, using an ability is inconspicuous. WordPress provides a function wp_get_ability() to retrieve the ability object thru name, and then you definitely definately call the ability’s execute() way.
Proper right here’s how you want to execute the site-analytics-summary method we registered:
$method = wp_get_ability( 'hongkiatcom/site-analytics-summary' );
if ( $method ) {
$consequence = $ability->execute();
}
If the ability requires inputs similar to inside the process-order example, you in all probability can transfer an associative array of inputs to the execute() way:
$method = wp_get_ability( 'hongkiatcom/process-order' );
if ( $method ) {
$inputs = [
'customer_id' => 123,
'product_skus' => [ 'SKU123', 'SKU456' ],
'payment_token'=> 'tok_1A2B3C****',
];
$consequence = $ability->execute( $inputs );
}
Understand that we didn’t will have to manually maintain permission checks or input validation proper right here. If the existing client didn’t have permission, or if we passed a mistaken type of input, execute() would fail gracefully. It wouldn’t run the callback and would return an error or false. This is as a result of the schemas and permission callback we prepare throughout registration. It makes using the ability secure and predictable.
Now that we’ve noticed how you’ll declare and use abilities on the back-end, let’s take a look at how abilities can be accessed from JavaScript, which covers front-end use cases and external integrations.
In JavaScript
The Skills API is helping REST API out of the sector, alternatively you need to just be sure you allow the show_in_rest on the Ability registration, for instance:
add_action( 'wp_abilities_api_init', function () {
if ( ! function_exists( 'wp_register_ability' ) ) {
return;
}
wp_register_ability(
'hongkiatcom/site-analytics-summary',
[
...
'execute_callback' => function () {
return [
'visits' => 1473,
'signups' => 32,
'sales' => 5,
];
},
'meta' => [
'show_in_rest' => true,
],
]
);
});
Once that’s set, the ability becomes accessible underneath a singular REST namespace wp-abilities/v1/{ability-namespace}/{ability-name}. In our example above, the full REST endpoint to execute the ability can also be: /wp-json/wp-abilities/v1/hongkiatcom/site-analytics-summary.
This means external programs or your own front-end code can execute abilities thru sending HTTP requests without you writing any further REST handler code. WordPress takes care of that in step with the tips you provided.
WordPress is also introducing a JavaScript client library for the Skills API, making it much more simple to call abilities from the browser or any headless JS setup. Instead of writing information fetch() calls, you’ll simply use built-in helper functions to tick list, fetch, and run abilities.
First, we’d need to arrange the library with NPM:
npm i @wordpress/abilities
Then, we can use it like this in our JavaScript tool:
useEffect(() => {
executeAbility( 'hongkiatcom/site-analytics-summary' ).then( ( response ) => {
setResponse( response ); // Store the result in state.
} );
}, []);
Wrapping Up
The Skills API introduces a brand spanking new method for WordPress to give an explanation for what it will in truth do in a clear and additional standardized construction. This improves the interoperability of your internet web site and allows it to artwork additional naturally with AI assistants or automation tools.
In this article, we’ve merely scratched the outside of what’s possible with the Skills API. As additional plugins and problems adopt it, we can expect a richer ecosystem where options are merely discoverable and usable all through different contexts.
And inside the next article, we’ll see how we can mix your WordPress internet web site with external programs like Claude or LM Studio using the Skills API.
So stay tuned!
The publish The way to Use the WordPress Talents API (Sign up & Execute) seemed first on Hongkiat.
Supply: https://www.hongkiat.com/blog/wordpress-abilities-api-tutorial/



![Lead Technology Content material: Best Sorts to Use in 2023 [Data + Expert Tips]](https://wpmountain.com/wp-content/uploads/2021/11/b0f73a5e-16e4-41fd-9511-8564efc560a7-150x59.png)


0 Comments