Created
June 25, 2020 21:08
-
-
Save sublayerio/3b75983cd2176eb32fc7d388e92a5545 to your computer and use it in GitHub Desktop.
WP REST Route - Add all terms route
This file contains hidden or 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
class all_terms | |
{ | |
public function __construct() | |
{ | |
$version = '2'; | |
$namespace = 'wp/v' . $version; | |
$base = 'all-terms'; | |
register_rest_route($namespace, '/' . $base, array( | |
'methods' => 'GET', | |
'callback' => array($this, 'get_all_terms'), | |
)); | |
} | |
public function get_all_terms($object) | |
{ | |
$return = array(); | |
// $return['categories'] = get_terms('category'); | |
// $return['tags'] = get_terms('post_tag'); | |
// Get taxonomies | |
$args = array( | |
'public' => true, | |
'_builtin' => false | |
); | |
$output = 'names'; // or objects | |
$operator = 'and'; // 'and' or 'or' | |
$taxonomies = get_taxonomies($args, $output, $operator); | |
foreach ($taxonomies as $key => $taxonomy_name) { | |
if($taxonomy_name = $_GET['term']){ | |
$return = get_terms($taxonomy_name); | |
} | |
} | |
return new WP_REST_Response($return, 200); | |
} | |
} | |
add_action('rest_api_init', function () { | |
$all_terms = new all_terms; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment