Created
June 8, 2023 07:21
-
-
Save AliNaraghiA/301e06d781a1bfe11d5f0df67e354c87 to your computer and use it in GitHub Desktop.
give the post id and returns the category
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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