Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //API fields
- add_action( 'rest_api_init', 'WPCHTML_create_api_fields' ); //or whatever name you give to the function below
- function WPCHTML_create_api_fields() {
- //register_rest_field ( 'name-of-post-type', 'name-of-field-to-return', array-of-callbacks-and-schema() )
- //for custom permalink
- register_rest_field( 'wpchtmlp_page', 'html_permalink', array(
- 'get_callback' => 'WPCHTMLP_get_api_post_meta_permalink', //or whatever you name the callback function below
- 'update_callback' => 'WPCHTMLP_update_api_post_meta_permalink', //name of update callback function at bottom
- 'schema' => null,
- )
- );
- //for HTML content
- register_rest_field( 'wpchtmlp_page', 'html_code', array(
- 'get_callback' => 'WPCHTMLP_get_api_post_meta_code', //or whatever you name the callback function below
- 'update_callback' => 'WPCHTMLP_update_api_post_meta_code', //name of update callback function at bottom
- 'schema' => null,
- )
- );
- }
- function WPCHTMLP_get_api_post_meta_permalink( $object, $field_name, $request ) {
- $meta = get_post_meta( $object['id'], 'WPCHTMLP_page_meta_box', true );
- //return the post meta
- return $meta['html_permalink'];
- }
- function WPCHTMLP_get_api_post_meta_code( $object, $field_name, $request ) {
- $meta = get_post_meta( $object['id'], 'WPCHTMLP_page_meta_box', true );
- //return the post meta
- return $meta['html_code'];
- }
- function WPCHTMLP_update_api_post_meta_permalink($value, $object, $field_name){
- return update_post_meta($object['id'], 'WPCHTMLP_page_meta_box', array('html_permalink', $value));
- }
- function WPCHTMLP_update_api_post_meta_code($value, $object, $field_name){
- return update_post_meta($object['id'], 'WPCHTMLP_page_meta_box', array('html_code', $value));
- }
Advertisement
Add Comment
Please, Sign In to add comment