Advertisement
Guest User

Gestione POST/CUSTOM POST

a guest
Mar 22nd, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  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>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement