daily pastebin goal
76%
SHARE
TWEET

Gestione POST/CUSTOM POST

a guest Mar 22nd, 2019 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3.     // Funzione per generare URL e NONCE
  4.     wp_localize_script( 'qzr-mainjs', 'my_restapi_details', array(
  5.         'rest_url' => esc_url_raw( rest_url() ),
  6.         'nonce' => wp_create_nonce( 'wp_rest' )
  7.     ) );
  8.  
  9. ?>
  10.  
  11. <script>
  12.     /*
  13.         Con POST    -> Si crea un nuovo post, occorre definire i campi da modificare. URL: wp/v2/custom_post
  14.         Con PUT     -> Modifica il post, occorre definire i campi da modificare. URL: wp/v2/custom_post/id
  15.         Con DELETE  -> Cancella il post. URL: wp/v2/custom_post/id
  16.     */
  17.  
  18.     if( $('.developer').length ) {
  19.         $.ajax({
  20.             method: 'POST',
  21.             url: my_restapi_details.rest_url + 'wp/v2/course',
  22.             data: {
  23.                 title: 'Giovanni truppi no',
  24.                 content: '[my_sample_shortcode]',
  25.                 type: 'post',
  26.                 status: 'publish',
  27.                 teachers: 'test',
  28.                 description: 'test2'
  29.             },
  30.             beforeSend: function ( xhr ) {
  31.                 xhr.setRequestHeader( 'X-WP-Nonce', my_restapi_details.nonce );
  32.             },
  33.             success : function( response ) {
  34.          
  35.                 // Save the post ID in case you need it for something
  36.          
  37.                 var myNewPostID = response.id;
  38.          
  39.             }
  40.         });
  41.     }
  42. </script>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top