Created
June 8, 2023 07:31
-
-
Save AliNaraghiA/86a14fedac20fc19b011b2a80b5ef425 to your computer and use it in GitHub Desktop.
give you all categories
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_all_category_names( $request ) { | |
$categories = get_categories( array( | |
'taxonomy' => 'category', | |
'hide_empty' => false, | |
) ); | |
$data = array(); | |
foreach ( $categories as $category ) { | |
$data[] = $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', '/all-category-names/', array( | |
'methods' => 'GET', | |
'callback' => 'get_all_category_names', | |
) ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment