Migrating image galleries from other site to wordpress
-
Hello everybody. I’m trying to upload/import old site galleries to WordPress with script as custom_field but after 5-6 galleries it gives 503 error. Maybe anybody knows how to optimize the code to do all galleries.
My server settings: max_execution_time:1200, memory_limit: 512M, post_max_size: 128M$info = file_get_contents('data.php');; $data = json_decode("$info",true); function Generate_Featured_Image( $image_url_arr, $post_id1, $image_date ){ ob_start(); foreach ($image_url_arr as $image_url){ $post_id = false; require_once(ABSPATH . 'wp-admin/includes/file.php'); $tmp = download_url( $image_url ); // Set variables for storage // fix file filename for query strings preg_match( '/[^\?]+\.(jpe?g|jpe|gif|png)\b/i', $image_url, $matches ); $file_array['name'] = basename($matches[0]); $file_array['tmp_name'] = $tmp; // If error storing temporarily, unlink if ( is_wp_error( $tmp ) ) { @unlink($file_array['tmp_name']); $file_array['tmp_name'] = ''; } $time = $image_date ; $file = wp_handle_sideload( $file_array, array('test_form'=>false), $time ); if ( isset($file['error']) ) { return new WP_Error( 'upload_error', $file['error'] ); } $url = $file['url']; $type = $file['type']; $file = $file['file']; $title = preg_replace('/\.[^.]+$/', '', basename($file) ); $parent = (int) absint( $post_id ) > 0 ? absint($post_id) : 0; $attachment = array( 'post_mime_type' => $type, 'guid' => $url, 'post_parent' => $parent, 'post_title' => $title, 'post_content' => '', ); $id = wp_insert_attachment($attachment, $file, $parent); if ( !is_wp_error($id) ) { require_once ABSPATH . 'wp-admin/includes/image.php'; $data = wp_generate_attachment_metadata( $id, $file ); wp_update_attachment_metadata( $id, $data ); } $res2[] = $id; } set_post_thumbnail( $post_id1, $res2[0] ); update_field('galerijos_nuotraukos', $res2, $post_id1 ); echo ob_get_contents(); ob_end_flush(); } $i=0; $my_post = array(); foreach ($data['title'] as $post1){ if (empty($make_data[$i])) { $make_data[$i] = "2016-05-01"; } $my_post = array( 'post_title' => $post1, 'post_content' =>'', 'post_status' => 'publish', 'post_type' => 'galerijos', 'post_date' => $make_data[$i], ); $image_date = str_replace("-","/",$data['data'][$i]); $post_id = wp_insert_post($my_post); Generate_Featured_Image( $data['img'][$i], $post_id, $image_date ); $i++; }
- The topic ‘Migrating image galleries from other site to wordpress’ is closed to new replies.