Guest User

Untitled

a guest
Feb 22nd, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. function imdb_ajax_action(){
  2. if (!wp_verify_nonce($data['imdb_ajax_nonce'], basename(__FILE__))){
  3. die(-1);
  4. }
  5.  
  6. $api_meta = null;
  7.  
  8. if ($data['imdb-box-title']){
  9. $search_title = $data['imdb-box-title'];
  10. $api_url = 'MY_API_URL';
  11. $api_response = json_decode(file_get_contents($api_url));
  12.  
  13. $api_meta = [
  14. 'year' => $api_response->Year,
  15. 'synopsis' => $api_response->Plot,
  16. 'director' => $api_response->Director,
  17. 'runtime' => $api_response->Runtime,
  18. 'rating' => $api_response->imdbRating,
  19. ];
  20. }
  21.  
  22. print json_encode($api_meta);
  23. wp_die();
  24. }
  25.  
  26. jQuery(document).ready(function($) {
  27. $('body').on('click', '#scrape-imdb', function (e) {
  28. e.preventDefault();
  29. var $me = $(this),
  30. action = 'imdb_ajax_action';
  31.  
  32. var data = $.extend(true, $me.data(), {
  33. action: action,
  34. form_data: $('#post').serializeArray()
  35. });
  36.  
  37. $.post(ajaxurl, data, function (response) {
  38. if (response == '0' || response == '-1'){
  39. console.error('IMDb AJAX error');
  40. console.log(response);
  41. } else {
  42. console.log('AJAX successful');
  43. var movie_data = $.parseJSON(response);
  44.  
  45. $("div[data-name='year']").find('input').val(movie_data.year);
  46. $("div[data-name='synopsis']").find('textarea').val(movie_data.synopsis);
  47. $("div[data-name='director']").find('input').val(movie_data.director);
  48. $("div[data-name='runtime']").find('input').val(movie_data.runtime);
  49. $("div[data-name='imdb_rating']").find('input').val(movie_data.rating);
  50. }
  51. });
  52. })
  53. });
Add Comment
Please, Sign In to add comment