my custom image upload function is causing 500 Connection Timeout Error
-
I wrote a custom script to insert a post into WordPress and upload 3 images to the WP uploads directory.
To write the post I use the WP function
wp_insert_post( $wp_post_array, true );
.
Inside the script at various stages I also usewp_get_attachment_image_src($image_id, $size)[0];
,wp_get_attachment_metadata($image_id);
andwp_get_attachment_image( $image_id, 'large', false, $image_attr );
but to upload the images and create their metadata I wrote this custom function below…I must have messed up somewhere because I get a 500 Connection Timeout error when I run this code (even though it is only 3 images that are less than 1Mb each in size).
Can somebody spot what I am doing wrong? Thank you for your eyes and experience.
function insert_WP_Images_Data( $post_id, $image_url ) { global $writer_WP_id; $upload_dir = wp_upload_dir(); if ( isset($image_url) && isset($post_id) ) { $filename = basename($image_url); if(wp_mkdir_p($upload_dir['path'])) $file = $upload_dir['path'] . '/' . $filename; else $file = $upload_dir['basedir'] . '/' . $filename; $image_data = file_get_contents( $image_url ); file_put_contents($file, $image_data); $wp_filetype = wp_check_filetype($filename, null); $attachment = array( 'post_author' => $writer_WP_id, 'post_content' => '', 'post_title' => $_SESSION['artist'], 'post_status' => 'inherit', 'post_name' => pathinfo($image_url)['filename'], 'post_mime_type' => $wp_filetype['type'], 'post_parent' => $post_id, 'guid' => $upload_dir['url'].'/'.$filename ); // 'post_title' => sanitize_file_name($filename), $image_id = wp_insert_attachment( $attachment, $file, $post_id ); require_once( ABSPATH.'wp-admin/includes/image.php' ); $attach_data = wp_generate_attachment_metadata( $image_id, $file ); $res1 = wp_update_attachment_metadata( $image_id, $attach_data ); $res2 = set_post_thumbnail( $post_id, $image_id ); return $image_id; } else { echo '<span class="error">No post is selected or image is selected</span>'; } }
I have already tried increasing my server execution time in cPanel (200, 600) and via .htaceess (300) but nothing works…
- The topic ‘my custom image upload function is causing 500 Connection Timeout Error’ is closed to new replies.