akcjaod 4.4.0
rest_api_init
Opis
Inicjalizuje REST API. Rejestruje własne trasy przez register_rest_route().
Przykład użycia
add_action( 'rest_api_init', ... )php
add_action( 'rest_api_init', 'wpdocs_register_rest_route' );
function wpdocs_register_rest_route() {
register_rest_route( 'wpdocs/v1', '/books', array(
'methods' => 'GET',
'callback' => function () {
return get_posts( array(
'post_type' => 'book',
'posts_per_page' => -1,
) );
},
'permission_callback' => '__return_true',
) );
}
add_action( 'rest_api_init', 'wpdocs_register_rest_route' );
function wpdocs_register_rest_route() {
register_rest_route( 'wpdocs/v1', '/books', array(
'methods' => 'GET',
'callback' => function () {
return get_posts( array(
'post_type' => 'book',
'posts_per_page' => -1,
) );
},
'permission_callback' => '__return_true',
) );
}Endpoint objects should be created and register their hooks on this action rather than another action to ensure they're only loaded when needed.
Parametry
| Parametr | Typ | Opis |
|---|---|---|
$wp_rest_server | \WP_REST_Server | Server object. |
Wywołanie
Rejestracja callbackaphp
// rejestracja funkcji zwrotnej
add_action( 'rest_api_init', 'twoja_funkcja' );
function twoja_funkcja($param_1) {
// Twój kod
}
// rejestracja funkcji zwrotnej
add_action( 'rest_api_init', 'twoja_funkcja' );
function twoja_funkcja($param_1) {
// Twój kod
}Popularne w tej kategorii