Last active
March 10, 2017 16:56
-
-
Save admataz/d46afeeed00e1765a557fa5cffd473af to your computer and use it in GitHub Desktop.
Adding a support to WP REST API for custom fields
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
| <?php | |
| function my_custom_fields_rest_support( $post ) { | |
| $custom = get_post_meta($post['id']); | |
| $fields = array(); | |
| foreach($custom as $key=>$value) { | |
| // Assuming Types - could be other Custom Field Types | |
| if(substr($key, 0, 5) == 'wpcf-'){ | |
| $fields[str_replace('wpcf-', '', $key)] = $value; | |
| } | |
| } | |
| return $fields; | |
| } | |
| add_action('rest_api_init', function(){ | |
| register_rest_field('factcheck-resource', 'fields', array('get_callback' => 'my_custom_fields_rest_support')); | |
| }); | |
| /** | |
| * Add REST API support to an already registered taxonomy. | |
| */ | |
| function my_custom_taxonomy_rest_support() { | |
| global $wp_taxonomies; | |
| $taxonomies = array('resource-country', 'resource-topic'); | |
| foreach($taxonomies as $t){ | |
| if ( isset( $wp_taxonomies[ $t ] ) ) { | |
| $wp_taxonomies[ $t ]->show_in_rest = true; | |
| } | |
| } | |
| } | |
| add_action( 'init', 'my_custom_taxonomy_rest_support', 25 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment