WordPress JSON Rest Api Add Custom Fields

8 Feb
2018

Open wp-includes/functions.php file and add:

    function custom_field_get_post_meta_cb($object, $field_name, $request){
			// return get_post_custom();

		$metas = get_post_custom($post_id);

		foreach($metas as $key => $value) {
			if(sizeof($value) == 1) {
			  $metas[$key] = $value[0];
			}
		}
		return $metas;			
    }
    function custom_field_update_post_meta_cb($value, $object, $field_name){
      return update_post_meta($object['id'], $field_name, $value); 
    }
    add_action('rest_api_init', function(){
      register_rest_field('post', 'custom_fields', array('get_callback' => 'custom_field_get_post_meta_cb')); 
    
    });
Be Sociable, Share!

Comment Form

top