Skip to content

Commit

Permalink
expose price to api
Browse files Browse the repository at this point in the history
  • Loading branch information
JeanSebTr committed Mar 11, 2020
1 parent c23243f commit 5acac30
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions wp-content/plugins/my_plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,19 @@ function generate_course_type() {
'rest_base' => 'courses',
);
register_post_type( 'course_type', $args );

$meta_args = array(
'type' => 'number',
'description' => 'The price of a course.',
'single' => true,
'show_in_rest' => true,
);
register_post_meta( 'course_type', 'price', $meta_args );

}
add_action( 'init', 'generate_course_type', 0 );

function get_price_field($object, $field_name, $value) {
return floatval(get_post_meta($object['id'])[$field_name][0]);
}

function register_course_price_in_api() {
register_rest_field('course_type', 'price', array(
'get_callback' => 'get_price_field',
'update_callback' => null,
'schema' => null,
));
}

add_action( 'rest_api_init', 'register_course_price_in_api' );

0 comments on commit 5acac30

Please sign in to comment.