通用API文档

💡 云策文档标注

概述

本文档介绍了 WordPress 6.9 中新增的 Abilities API,包括如何检查其可用性、注册自定义能力以及执行能力的基本用法。

关键要点

  • Abilities API 在 WordPress 6.9 中引入,使用前需检查 WP_Ability 类是否存在以确保向后兼容性。
  • 通过 wp_abilities_api_init 钩子注册能力,使用 wp_register_ability 函数定义能力属性,如标签、描述、输出模式和回调函数。
  • 能力执行涉及 wp_get_ability 获取能力对象,并通过 execute 方法调用,需处理可能返回的 WP_Error。

代码示例

if ( ! class_exists( 'WP_Ability' ) ) {
    add_action(
        'admin_notices',
        static function () {
            wp_admin_notice(
                esc_html__( 'This plugin requires the Abilities API, which is only available in WordPress 6.9 or newer. Please update your WordPress version to use this plugin.', 'textdomain' ),
                'error'
            );
        }
    );
    return;
}

add_action( 'wp_abilities_api_init', 'wporg_register_abilities' );
function wporg_register_abilities() {
    wp_register_ability(
        'wporg/get-site-title',
        array(
            'label'               => __( 'Get Site Title', 'textdomain' ),
            'description'         => __( 'Retrieves the title of the current WordPress site.', 'textdomain' ),
            'output_schema'       => array(
                'type'        => 'string',
                'description' => 'The site title.',
            ),
            'execute_callback'    => 'wporg_get_site_title',
            'permission_callback' => '__return_true',
            'meta'                => array(
                'category'     => 'site-info',
                'show_in_rest' => true,
            ),
        )
    );
}

function wporg_get_site_title(): string {
    return get_bloginfo( 'name' );
}

add_action( 'admin_init', 'wporg_use_ability' );
function wporg_use_ability() {
    $ability = wp_get_ability( 'wporg/get-site-title' );
    if ( ! $ability ) {
        return;
    }
    $site_title = $ability->execute();
    if ( is_wp_error( $site_title ) ) {
        error_log( 'Execution error: ' . $site_title->get_error_message() );
        return;
    }
    echo 'Site Title: ' . esc_html( $site_title );
}

📄 原文内容

The Abilities API was added to WordPress in version 6.9.

For backwards compatibility, you can check if the WP_Ability class is available.

if ( ! class_exists( 'WP_Ability' ) ) {
	// E.g. add an admin notice about the missing dependency.
	add_action(
		'admin_notices',
		static function () {
			wp_admin_notice(
				esc_html__( 'This plugin requires the Abilities API, which is only available in WordPress 6.9 or newer. Please update your WordPress version to use this plugin.', 'textdomain' ),
				'error'
			);
		}
	);

	return;
}

Basic Usage Example

The below example is for a plugin implementation, but it could also be adapted for a theme’s functions.php

<?php
// 1. Register the ability when the Abilities API is initialized.
// Using `wp_abilities_api_init` ensures the API is fully loaded.
add_action( 'wp_abilities_api_init', 'wporg_register_abilities' );
/**
 * Register custom abilities.
 *
 * @return void
 */
function wporg_register_abilities() {
	wp_register_ability(
		'wporg/get-site-title',
		array(
			'label'               => __( 'Get Site Title', 'textdomain' ),
			'description'         => __( 'Retrieves the title of the current WordPress site.', 'textdomain' ),
			'output_schema'       => array(
				'type'        => 'string',
				'description' => 'The site title.',
			),
			'execute_callback'    => 'wporg_get_site_title',
			'permission_callback' => '__return_true', // Everyone can access this.
			'meta'                => array(
				'category'     => 'site-info',
				'show_in_rest' => true, // Optional: expose via REST API.
			),
		)
	);
}
// 2. Define a callback function for your ability.
/**
 * Callback to get the site title.
 *
 * @return string
 */
function wporg_get_site_title(): string {
	return get_bloginfo( 'name' );
}


// 3. Later, you can retrieve and execute the ability.
add_action( 'admin_init', 'wporg_use_ability' );
/**
 * Use the registered ability.
 *
 * @return void
 */
function wporg_use_ability() {
	$ability = wp_get_ability( 'wporg/get-site-title' );
	if ( ! $ability ) {
		// Ability not found.
		return;
	}

	$site_title = $ability->execute();
	if ( is_wp_error( $site_title ) ) {
		// Handle execution error.
		error_log( 'Execution error: ' . $site_title->get_error_message() );
		return;
	}

	// `$site_title` now holds the result of `get_bloginfo( 'name' )`.
	echo 'Site Title: ' . esc_html( $site_title );
}