rest_api_init
This is a WordPress - rest_api_init hook. The plugin just uses it.
Fires at the beginning of REST API request processing (when serving the REST request is being prepared).
New REST routes should be registered while this hook fires (see register_rest_route()) to ensure that they are available when a REST request is processed.
Usage
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)
- The REST server object.
Examples
#1 Create a new endpoint for a plugin
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;
}
After installing this code, the endpoint:
GET http://example.com/wp-json/myplug/v2/posts
Will return the site's posts in the specified format.
Where the hook is called
rest_api_init
woocommerce/includes/cli/class-wc-cli-runner.php 59
do_action( 'rest_api_init', $wp_rest_server );
Where the hook is used in 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 351
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 48
add_action( 'rest_api_init', array( $this, 'rest_api_init' ) );
woocommerce/src/Admin/Features/Blueprint/Init.php 43
add_action( 'rest_api_init', array( $this, 'init_rest_api' ) );
woocommerce/src/Api/Infrastructure/Main.php 178
add_action( 'rest_api_init', array( self::class, 'handle_rest_api_init_for_core' ) );
woocommerce/src/Api/Infrastructure/Main.php 287
add_action( 'rest_api_init', array( $registrar, 'handle_rest_api_init' ) );
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 151
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 43
add_action( 'rest_api_init', array( $this, 'register_routes' ) );