Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save AliNaraghiA/301e06d781a1bfe11d5f0df67e354c87 to your computer and use it in GitHub Desktop.
Save AliNaraghiA/301e06d781a1bfe11d5f0df67e354c87 to your computer and use it in GitHub Desktop.
give the post id and returns the category
<?php
/**
* Plugin Name:Ali Custom API
* Plugin URI:
* Description: Crushing
* Version: 1.0
* Author: Ali
* Author URI:
*/
function get_post_categories( $request ) {
$post_id = $request['id'];
$categories = get_the_category( $post_id );
$data = array();
foreach ( $categories as $category ) {
$data[] = array(
'id' => $category->term_id,
'name' => $category->name,
);
}
return new WP_REST_Response( $data, 200 );
}
add_action( 'rest_api_init', 'register_custom_endpoints' );
function register_custom_endpoints() {
register_rest_route( 'myplugin/v1', '/post-categories/(?P<id>\d+)', array(
'methods' => 'GET',
'callback' => 'get_post_categories',
) );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment