PodsRESTHandlers::taxonomy_rest_support PHP Method

taxonomy_rest_support() public static method

Add REST API support to an already registered taxonomy.
Since: 2.5.6
public static taxonomy_rest_support ( string $taxonomy_name, boolean | false $rest_base = false, string $controller = 'WP_REST_Terms_Controller' )
$taxonomy_name string Taxonomy name.
$rest_base boolean | false Optional. Base url segment. If not set, taxonomy name is used.
$controller string Optional, controller class for route. If not set "WP_REST_Terms_Controller" is used.
    public static function taxonomy_rest_support($taxonomy_name, $rest_base = false, $controller = 'WP_REST_Terms_Controller')
    {
        global $wp_taxonomies;
        if (isset($wp_taxonomies[$taxonomy_name])) {
            if (!$rest_base) {
                $rest_base = $taxonomy_name;
            }
            $wp_taxonomies[$taxonomy_name]->show_in_rest = true;
            $wp_taxonomies[$taxonomy_name]->rest_base = $rest_base;
            $wp_taxonomies[$taxonomy_name]->rest_controller_class = $controller;
        }
    }

Usage Example

示例#1
0
 /**
  * Add REST API support to post type and taxonomy objects.
  *
  * @uses "init"
  *
  * @since 2.5.6
  */
 public function add_rest_support()
 {
     static $rest_support_added;
     if (!function_exists('register_rest_field')) {
         return;
     }
     include_once PODS_DIR . 'classes/PodsRESTFields.php';
     include_once PODS_DIR . 'classes/PodsRESTHandlers.php';
     $rest_bases = pods_transient_get('pods_rest_bases');
     if (empty($rest_bases)) {
         $pods = pods_api()->load_pods();
         $rest_bases = array();
         if (!empty($pods) && is_array($pods)) {
             foreach ($pods as $pod) {
                 $type = $pod['type'];
                 if (in_array($type, array('post_type', 'taxonomy'))) {
                     if ($pod && PodsRESTHandlers::pod_extends_core_route($pod)) {
                         $rest_bases[$pod['name']] = array('type' => $type, 'base' => sanitize_title(pods_v('rest_base', $pod['options'], $pod['name'])));
                     }
                 }
             }
         }
         if (empty($rest_bases)) {
             $rest_bases = 'none';
         }
         pods_transient_set('pods_rest_bases', $rest_bases);
     }
     if (empty($rest_support_added) && !empty($rest_bases) && 'none' !== $rest_bases) {
         foreach ($rest_bases as $pod_name => $pod_info) {
             $pod_type = $pod_info['type'];
             $rest_base = $pod_info['base'];
             if ('post_type' == $pod_type) {
                 PodsRESTHandlers::post_type_rest_support($pod_name, $rest_base);
             } elseif ('taxonomy' == $pod_type) {
                 PodsRESTHandlers::taxonomy_rest_support($pod_name, $rest_base);
             }
             new PodsRESTFields($pod_name);
         }
         $rest_support_added = true;
     }
 }