Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function imdb_ajax_action(){
- if (!wp_verify_nonce($data['imdb_ajax_nonce'], basename(__FILE__))){
- die(-1);
- }
- $api_meta = null;
- if ($data['imdb-box-title']){
- $search_title = $data['imdb-box-title'];
- $api_url = 'MY_API_URL';
- $api_response = json_decode(file_get_contents($api_url));
- $api_meta = [
- 'year' => $api_response->Year,
- 'synopsis' => $api_response->Plot,
- 'director' => $api_response->Director,
- 'runtime' => $api_response->Runtime,
- 'rating' => $api_response->imdbRating,
- ];
- }
- print json_encode($api_meta);
- wp_die();
- }
- jQuery(document).ready(function($) {
- $('body').on('click', '#scrape-imdb', function (e) {
- e.preventDefault();
- var $me = $(this),
- action = 'imdb_ajax_action';
- var data = $.extend(true, $me.data(), {
- action: action,
- form_data: $('#post').serializeArray()
- });
- $.post(ajaxurl, data, function (response) {
- if (response == '0' || response == '-1'){
- console.error('IMDb AJAX error');
- console.log(response);
- } else {
- console.log('AJAX successful');
- var movie_data = $.parseJSON(response);
- $("div[data-name='year']").find('input').val(movie_data.year);
- $("div[data-name='synopsis']").find('textarea').val(movie_data.synopsis);
- $("div[data-name='director']").find('input').val(movie_data.director);
- $("div[data-name='runtime']").find('input').val(movie_data.runtime);
- $("div[data-name='imdb_rating']").find('input').val(movie_data.rating);
- }
- });
- })
- });
Add Comment
Please, Sign In to add comment