Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save AliNaraghiA/86a14fedac20fc19b011b2a80b5ef425 to your computer and use it in GitHub Desktop.
Save AliNaraghiA/86a14fedac20fc19b011b2a80b5ef425 to your computer and use it in GitHub Desktop.
give you all categories
<?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