Monday, December 12, 2022
HomeWordPress Developmentphp - REST API customized endpoints for metaboxes

php – REST API customized endpoints for metaboxes


I’ve a 3 metaboxes related to my customized publish sort “cpt-project”. I wish to fetch the information to point out it within the JSON utilizing the REST API.

I perceive that I’ve to create customized endpoints to retrieve the date. Nevertheless I’ve simply found the REST API and I am positive from the place to begin to obtain it.

Right here is the PHP code for the metaboxes:

add_action( 'add_meta_boxes', 'mysite_area_add_meta_box' );
  if ( ! function_exists( 'mysite_area_add_meta_box' ) ) {
   /**
    * Add meta field to web page display
    *
    * @since 1.0.0
    */
 
   perform mysite_area_add_meta_box() {
    add_meta_box( 'additional-area-metabox-options', esc_html__( 'Textual content', 'wp-felix-ruiz-fotografia' ), 'mysite_area_metabox_controls', 'cpt-project', 'regular', 'excessive' );
   }
  }
 
  if ( ! function_exists( 'mysite_area_metabox_controls' ) ) {
   /**
    * Meta field render perform
    *
    * @param  object $publish Submit object.
    * @since  1.0.0
    */
 
   perform mysite_area_metabox_controls( $publish ) {

     $meta = get_post_meta( $post->ID );
     $text_1 = ( isset( $meta['text_1'][0] ) && '' !== $meta['text_1'][0] ) ? $meta['text_1'][0] : '';
     $text_2 = ( isset( $meta['text_2'][0] ) && '' !== $meta['text_2'][0] ) ? $meta['text_2'][0] : '';
     $text_3 = ( isset( $meta['text_3'][0] ) && '' !== $meta['text_3'][0] ) ? $meta['text_3'][0] : '';

     wp_nonce_field( 'mysite_area_control_meta_box', 'mysite_area_control_meta_box_nonce' ); // At all times add nonce to your meta packing containers!
     ?>
     <div class="post_meta_extras">
      <p>
        <textarea sort="textual content" title="text_1" id="" class="widefat" cols="100%" rows="5"><?php echo esc_attr( $text_1 ); ?></textarea>
      </p>

      <p>
        <textarea sort="textual content" title="text_2" id="" class="widefat" cols="100%" rows="5"><?php echo esc_attr( $text_2 ); ?></textarea>
      </p>

      <p>
        <textarea sort="textual content" title="text_3" id="" class="widefat" cols="100%" rows="5"><?php echo esc_attr( $text_3 ); ?></textarea>
      </p>
 
       <?php
   }
  }
 
  add_action( 'save_post', 'mysite_area_save_metaboxes' );
  if ( ! function_exists( 'mysite_area_save_metaboxes' ) ) {
   /**
    * Save controls from the meta packing containers
    *
    * @param  int $post_id Present publish id.
    * @since 1.0.0
    */
   perform mysite_area_save_metaboxes( $post_id ) {
 
     if ( ! isset( $_POST['mysite_area_control_meta_box_nonce'] ) || ! wp_verify_nonce( sanitize_key( $_POST['mysite_area_control_meta_box_nonce'] ), 'mysite_area_control_meta_box' ) ) {
       return $post_id;
     }
 
     if ( isset( $_POST['post_type'] ) && 'web page' === $_POST['post_type'] ) {
       if ( ! current_user_can( 'edit_page', $post_id ) ) {
         return $post_id;
       }
     } else {
       if ( ! current_user_can( 'edit_post', $post_id ) ) {
         return $post_id;
       }
     }
 
     if ( outlined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
       return $post_id;
     }
 
     if ( isset( $_POST['text_1'] ) ) {
      update_post_meta( $post_id, 'text_1', $_POST['text_1'] );
     }

     if ( isset( $_POST['text_2'] ) ) {
      update_post_meta( $post_id, 'text_2', $_POST['text_2'] );
     }

     if ( isset( $_POST['text_3'] ) ) {
      update_post_meta( $post_id, 'text_3', $_POST['text_3'] );
     }
 
   }
  }

Any assist can be a lot appreciated. Thanks.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments