Guest User

WP Custom HTML Pages plugin API support

a guest
Nov 14th, 2019
383
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.83 KB | None | 0 0
  1. //API fields
  2.  
  3. add_action( 'rest_api_init', 'WPCHTML_create_api_fields' ); //or whatever name you give to the function below
  4.  
  5. function WPCHTML_create_api_fields() {
  6.  
  7.     //register_rest_field ( 'name-of-post-type', 'name-of-field-to-return', array-of-callbacks-and-schema() )
  8.     //for custom permalink
  9.     register_rest_field( 'wpchtmlp_page', 'html_permalink', array(
  10.            'get_callback'    => 'WPCHTMLP_get_api_post_meta_permalink', //or whatever you name the callback function below
  11.            'update_callback' => 'WPCHTMLP_update_api_post_meta_permalink', //name of update callback function at bottom
  12.            'schema'          => null,
  13.         )
  14.     );
  15.     //for HTML content
  16.     register_rest_field( 'wpchtmlp_page', 'html_code', array(
  17.            'get_callback'    => 'WPCHTMLP_get_api_post_meta_code', //or whatever you name the callback function below
  18.            'update_callback' => 'WPCHTMLP_update_api_post_meta_code', //name of update callback function at bottom
  19.            'schema'          => null,
  20.         )
  21.     );
  22. }
  23.  
  24. function WPCHTMLP_get_api_post_meta_permalink( $object, $field_name, $request ) {
  25.     $meta = get_post_meta( $object['id'], 'WPCHTMLP_page_meta_box', true );
  26.     //return the post meta
  27.     return $meta['html_permalink'];
  28. }
  29.  
  30. function WPCHTMLP_get_api_post_meta_code( $object, $field_name, $request ) {
  31.     $meta = get_post_meta( $object['id'], 'WPCHTMLP_page_meta_box', true );
  32.     //return the post meta
  33.     return $meta['html_code'];
  34. }
  35.  
  36. function WPCHTMLP_update_api_post_meta_permalink($value, $object, $field_name){
  37.   return update_post_meta($object['id'], 'WPCHTMLP_page_meta_box', array('html_permalink', $value));
  38. }
  39.  
  40. function WPCHTMLP_update_api_post_meta_code($value, $object, $field_name){
  41.  return update_post_meta($object['id'], 'WPCHTMLP_page_meta_box', array('html_code', $value));
  42. }
Advertisement
Add Comment
Please, Sign In to add comment