前端提交通过外部url上传图片

时间:2019-12-05 作者:capkronos00

我有AJAX表单,可以通过电影id获取电影数据,并将这些数据发布到新的帖子中。

这是我的功能,它工作得很好,但我无法通过外部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 个回复
最合适的回答,由SO网友:Chetan Vaghela 整理而成

对于从外部url上载,应使用media_sideload_image 作用我想$posterurl 是外部图像的url,位于wp_insert_post 在中添加以下代码if($movie_id){} 块它会将图像从外部url上载到此帖子,如果您想将此图像设置为帖子功能图像,请使用set_post_thumbnail 否则请对该行进行注释。并将图像url另存为中的post meta字段poster_path 元键。

# $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)));
}

相关推荐

Extra "uploads" added in path

虽然这与高级自定义字段相关,但此函数使用所有本机WP特性,因此我认为在这里询问这一点是合适的。路径中的额外“/上传/”来自何处?在后端,我看到了上传文件的正确链接(domain.com/wp-content/member-files/name.pdf),但在前端,URL在路径(domain.com/wp-content/uploads/member-files/name.pdf)中显示了额外的“/uploads/”,当然为文件生成了404。// file upload to custom location