我有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\');