1

I'm trying to get the movie info from the omdb web service and it worked fine but it doesn't work when I want to use wp_insert_post to create new posts. This script acts as the user enters the movie ID and receives the information as an AJAX and a link is displayed to the user to edit the post but the post id value is 0.

function omdb_Flex_movie(){
add_menu_page(
    'Fetch info',
    'fetch info',
    'edit_posts',
    'test-script',
    'menu_callback',
    'dashicons-format-video',
    1);
}
add_action('admin_menu','omdb_Flex_movie');

function fetch_info(){

    if (current_user_can('edit_posts')) {
        if (isset($_POST['id_movie'])) {
            $id_movie = $_POST["id_movie"];
            if (($id_movie != NULL)) {
                $get_json_movie = file_get_contents("http://www.omdbapi.com/?apikey=ba9e1d33&i=$id_movie");
                $result = json_decode($get_json_movie, TRUE);
                $imdb = $result['imdbID'];
                $genrefarsi = $result['Genre'];
                $my_post = array(
                    'post_title' => 'test title',
                    'post_status' => 'pending',
                    'post_type' => 'post'
                );
                global $wpdb;
                $consulta = "SELECT meta_value FROM $wpdb->postmeta WHERE meta_key = 'id_imdb' AND meta_value = '{$imdb}' ";
                $verificar = $wpdb->get_results($consulta, OBJECT);
                if ($verificar) {
                    echo '<strong>This Post Already Exist</strong> ';
                } else {
                    $post_id = wp_insert_post($my_post);
                    echo '<a target="_blank" href="' . esc_url(home_url()) . '/wp-admin/post.php?post=' . $post_id . '&action=edit"><span class="draft_ready">Click This Link</span></a><span class="import_completed">Success</span>';
                }
                wp_set_object_terms($post_id, $genrefarsi, 'category');
            }
        }
    }

die();
}
add_action('wp_ajax_fetch_info', 'fetch_info');

function menu_callback(){
   ?>
        <!--Tabs Container-->
     <div class="tabs-container">
      <ul class="tabs">
        <li data-tab="film" class="active">movie</li>
        <li data-tab="serial" class="">series</li>
      </ul>
    </div>
   <!--Body Container-->
   <div class="omdb-container active" id="film">
      <div class="form">
       <div class="inputs">
        <input type="text" name="id_movie" placeholder="ID imdb">
        <?php wp_nonce_field('send-movies', 'send-movies-nonce'); ?>
        </div>
        <div class="bottom">
        <button type="button" id="sendAjax">Fetch info</button>
        <p>
        <small>exp :</small>
        http://www.imdb.com/title/<strong>tt2386490</strong>
        </p>
        </div>
        </div>
        <div class="loading"><img src="<?php echo DT_DIR_URI ;?>/inc/omdb/img/loading.svg"></div>
        <div class="result"></div>
        </div>
      }
1

I guess the reason is you have not added post title and added post id

$my_post = array(
                'post_id' => 1,
                'post_status' => 'pending',
                'post_type' => 'post'
            );

Ideally if should be

$my_post = array(
  'post_title'    => wp_strip_all_tags( $_POST['post_title'] ),
  'post_content'  => $_POST['post_content'],
  'post_status'   => 'publish',
  'post_author'   => 1,
  'post_category' => array( 8,39 ),
'post_type' => 'post'
);
  • thanks worked, but when i use if (isset($_POST['send-movies-nonce']) and wp_verify_nonce($_POST['send-movies-nonce'], 'send-movies')) not working – Brock_s34 Oct 17 at 8:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.