|
<?php // esta linea no la copies en tu functions.php |
|
|
|
// Vamos a crear la ruta /stats con info numérica adicional de tu WordPress |
|
// Para usarla, visita la URL tuweb.com/wp-json/wp/v2/stats |
|
|
|
// hacemos el hook al iniciar la API REST |
|
add_filter( 'rest_api_init', 'register_route_stats' ); |
|
|
|
// usamos register_rest_route para registrar la nueva ruta /stats |
|
function register_route_stats() { |
|
|
|
$api_namespace = 'wp/v2'; |
|
$stats_route = '/stats'; |
|
|
|
register_rest_route( $api_namespace, $stats_route, array( |
|
array( |
|
'methods' => WP_REST_Server::READABLE, // esto indica que la llamada es GET |
|
'callback' => 'get_stats', |
|
) |
|
) ); |
|
} |
|
|
|
// Aquí devolvemos el array con las estadísticas de /stats |
|
function get_stats() { |
|
$rest_url = get_rest_url() . $api_namespace . $stats_route; |
|
$rest_stats = array(); |
|
|
|
$count_posts = absint( wp_count_posts()->publish ); |
|
$count_pages = absint( wp_count_posts('page')->publish ); |
|
$count_comments = wp_count_comments()->total_comments; |
|
$count_users = count_users()['total_users']; |
|
$count_tags = absint( wp_count_terms( 'post_tag' ) ); |
|
$count_categories = absint( wp_count_terms( 'category' ) ); |
|
|
|
$rest_stats['posts'] = $count_posts; |
|
$rest_stats['pages'] = $count_pages; |
|
$rest_stats['comments'] = $count_comments; |
|
$rest_stats['users'] = $count_users; |
|
$rest_stats['tags'] = $count_tags; |
|
$rest_stats['categories'] = $count_categories; |
|
$rest_stats['meta']['links']['self'] = $rest_url; |
|
|
|
return $rest_stats; |
|
} |