I have AJAX form to get movie data by movie id and post this data into new post.

This is my function, It's works very well, But i can't upload image via external URL.

// Image URL

$poster = '//domain.com/image.jpg';

// Submit Function
function movie_grabber() {
    if(isset($_POST['movie_nonce_field']) && wp_verify_nonce($_POST['movie_nonce_field'], 'movie_nonce')) {
        if(strlen(trim($_POST['title'])) < 1 || strlen(trim($_POST['overview'])) < 1) {
            $redirect = add_query_arg('post', 'failed', home_url($_POST['_wp_http_referer']));
        } else {
            $user_id = get_current_user_id();
            $yearlist = $_POST['year'];
            $posterurl = $_POST['poster'];
            $yearex = explode(',', $yearlist);
            $genrelist =$_POST['genres'];
            $genreex = explode(',', $genrelist);
            $actorlist =$_POST['actors'];
            $actorex = explode(',', $actorlist);
            $directorlist =$_POST['director'];
            $directorex = explode(',', $directorlist);
            $countrylist =$_POST['country'];
            $countryex = explode(',', $countrylist);
            $movie_info = array(
                'post_title' => esc_attr(strip_tags($_POST['title'])),
                'post_type' => 'movie',
                'post_content' => esc_attr(strip_tags($_POST['overview'])),
                'post_status' => 'pending',
                'author' => $user_id,
                'tax_input'     => array(
                    'movie_year' => $yearex,
                    'movie_genre' => $genreex,
                    'movie_actor' => $actorex,
                    'movie_director' => $directorex,
                    'movie_country' => $countryex,
            $movie_id = wp_insert_post($movie_info);

            if($movie_id) {
                update_post_meta($movie_id, 'backdrop_path', esc_attr(strip_tags($_POST['backdrop_path'])));
                update_post_meta($movie_id, 'poster_path', esc_attr(strip_tags($_POST['poster'])));
                update_post_meta($movie_id, 'original_title', esc_attr(strip_tags($_POST['original_title'])));
                update_post_meta($movie_id, 'imdb_id', esc_attr(strip_tags($_POST['imdb'])));
                update_post_meta($movie_id, 'imdbrating', esc_attr(strip_tags($_POST['imdbrating'])));
                update_post_meta($movie_id, 'imdbvotes', esc_attr(strip_tags($_POST['imdbvotes'])));
                update_post_meta($movie_id, 'mpaa', esc_attr(strip_tags($_POST['mpaa'])));
                update_post_meta($movie_id, 'runtime', esc_attr(strip_tags($_POST['runtime'])));
                update_post_meta($movie_id, 'trailer_id', esc_attr(strip_tags($_POST['trailer_link'])));
                $redirect = add_query_arg('post', 'successfull', home_url($_POST['_wp_http_referer']));
        wp_redirect($redirect); exit;
add_action('init', 'movie_grabber');

1 Answer 1


For upload from external url you should use media_sideload_image function. I assume $posterurl is url of external image, after wp_insert_post add below code in if($movie_id){} block. It will upload image from external url to this post and if you want to set this image as post feature image use set_post_thumbnail otherwiase comment that line. and save a image url as post meta field in poster_path meta key.

# $posterurl third party url
if(!empty($posterurl)) {
  require_once(ABSPATH . 'wp-admin/includes/media.php');
  require_once(ABSPATH . 'wp-admin/includes/file.php');
  require_once(ABSPATH . 'wp-admin/includes/image.php');

  $attach_id = media_sideload_image($posterurl,$movie_id,'','id');
  # set post thumbnail for post
  set_post_thumbnail( $movie_id, $attach_id );
  # save meta field for poster path
  update_post_meta($movie_id, 'poster_path', esc_attr(strip_tags($posterurl)));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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