rest_api_init
Это хук WordPress - rest_api_init. Плагин его просто использует.
Срабатывает в начале обработки REST API-запроса (когда подготавливается обслуживание REST-запроса).
Во время срабатывания этого хука нужно регистрировать новые REST-маршруты (см. register_rest_route()), чтобы быть уверенным, что они будут срабатывать в момент REST-запроса.
Использование
add_action( 'rest_api_init', 'wp_kama_rest_api_init_action' );
/**
* Function for `rest_api_init` action-hook.
*
* @param WP_REST_Server $wp_rest_server Server object.
*
* @return void
*/
function wp_kama_rest_api_init_action( $wp_rest_server ){
// action...
}
- $wp_rest_server(WP_REST_Server)
- Объект REST сервера.
Примеры
#1 Создание новой конечной точки для своего плагина
add_action( 'rest_api_init', function(){
register_rest_route( 'myplug/v2', '/posts', array(
'methods' => 'GET',
'callback' => 'myplug_get_post_items',
) );
} );
function myplug_get_post_items(){
$posts = get_posts( array (
'post_status' => 'publish',
'numberposts' => 100
) ) ;
$items = array();
foreach( $posts as $post ){
$items[] = array(
'id' => $post->ID,
'title' => $post->post_title,
'author' => get_the_author_meta( 'display_name', $post->post_author ),
'content' => apply_filters( 'the_content', $post->post_content ),
'teaser' => $post->post_excerpt
);
}
return $items;
}
После установки этого кода, эндпоинт:
GET http://example.com/wp-json/myplug/v2/posts
Будет отдавать посты сайта в указанном формате.
Где вызывается хук
rest_api_init
woocommerce/includes/cli/class-wc-cli-runner.php 59
do_action( 'rest_api_init', $wp_rest_server );
Где используется хук в WooCommerce
woocommerce/includes/admin/class-wc-admin-marketplace-promotions.php 80
add_action( 'rest_api_init', array( __CLASS__, 'register_rest_routes' ) );
woocommerce/includes/admin/helper/class-wc-helper-admin.php 46
add_filter( 'rest_api_init', array( __CLASS__, 'register_rest_routes' ) );
woocommerce/includes/admin/helper/class-wc-helper-orders-api.php 24
add_filter( 'rest_api_init', array( __CLASS__, 'register_rest_routes' ) );
woocommerce/includes/admin/helper/class-wc-helper-subscriptions-api.php 26
add_filter( 'rest_api_init', array( __CLASS__, 'register_rest_routes' ) );
woocommerce/includes/class-wc-brands.php 65
add_action( 'rest_api_init', array( $this, 'rest_api_register_routes' ) );
woocommerce/includes/class-woocommerce.php 363
add_action( 'rest_api_init', array( $this, 'register_wp_admin_settings' ) );
woocommerce/includes/rest-api/Server.php 49
add_action( 'rest_api_init', array( $this, 'register_rest_routes' ), 10 );
woocommerce/packages/email-editor/src/Engine/class-email-editor.php 121
add_action( 'rest_api_init', array( $this, 'register_email_editor_api_routes' ) );
woocommerce/src/Admin/API/Init.php 49
add_action( 'rest_api_init', array( $this, 'rest_api_init' ) );
woocommerce/src/Admin/Features/Blueprint/Init.php 42
add_action( 'rest_api_init', array( $this, 'init_rest_api' ) );
woocommerce/src/Blocks/BlockTypes/Checkout.php 43
add_action( 'rest_api_init', array( $this, 'register_settings' ) );
woocommerce/src/Blocks/BlockTypes/ProductCollection/Controller.php 74
add_action( 'rest_api_init', array( $this, 'register_settings' ) );
woocommerce/src/Blocks/Shipping/ShippingController.php 73
add_action( 'rest_api_init', array( $this, 'register_settings' ) );
woocommerce/src/Internal/Admin/TaxSettingsRecommendations.php 36
add_action( 'rest_api_init', array( $this, 'register_routes' ) );
woocommerce/src/Internal/Admin/WCAdminUser.php 21
add_action( 'rest_api_init', array( $this, 'register_user_data' ) );
woocommerce/src/Internal/EmailEditor/Integration.php 180
add_action( 'rest_api_init', array( $this->email_api_controller, 'register_routes' ) );
woocommerce/src/Internal/MCP/MCPAdapterProvider.php 55
add_action( 'rest_api_init', array( $this, 'maybe_initialize' ), 10 );
woocommerce/src/Internal/ProductFeed/Integrations/POSCatalog/POSIntegration.php 75
add_action( 'rest_api_init', array( $this, 'rest_api_init' ) );
woocommerce/src/Internal/PushNotifications/Controllers/PushNotificationRestController.php 44
add_action( 'rest_api_init', array( $this, 'register_routes' ) );