wp_generate_attachment_metadata returns empty array
-
Hello,
I receive a empty array when i use wp_generate_attachment_metadata and this function do not generates the intermediate images.
I am adding images using wp_handle_uploads and this works fine. This generates a attach_id that I use with the function wp_generate_attachment_metadata.
I get no errors or warnings in the error_reporting log.I am using WMPU(multisite) wordpress and I no longer know what to do…
$i=0; $imgpath =wp_upload_dir(); require_once( ABSPATH . 'wp-admin/includes/image.php' ); foreach ($images as $key) { //$images[] <-- images url in the default upload dir if($key == 'error')continue; $path = $imgpath['path'].'/'.basename($key); $attachment = array( 'guid' => $key, //url 'post_mime_type' => image_type_to_mime_type(exif_imagetype($key)), 'post_title' => sanitize_file_name(basename($key)), 'post_content' => '', 'post_status' => 'inherit' ); $attach_id = wp_insert_attachment( $attachment,$path, $pid ); $attach_dat = wp_generate_attachment_metadata( $attach_id, $path); if(!empty($attach_dat))wp_update_attachment_metadata($attach_id, $attach_dat); if($i==0) set_post_thumbnail( $pid, $attach_id ); $i++; }
That was coded in a non template-file, where i load WP with
require_once($_SERVER['DOCUMENT_ROOT']."/test/site0/wp-config.php");
I not sure it influences in something.
-
If you’re going to load WP that way (which is highly discouraged), start with wp-load.php. Without that,
ABSPATH
is not correctly defined.Recommended ways to initiate code is either through plugins, page templates, AJAX, or through admin-post.php. But if you’re not expecting your code to work on a different installation, wp-load.php will work.
If that doesn’t help, there’s probably a path problem somewhere, check them carefully. Avoid relative paths unless you’re absolutely sure what it’s relative to. Be sure the attachment ID actually belongs to a valid entry in the posts table.
I verified everything, but not looks like wrong.
The image upload is done by two steps:
First the images are dynamically send to server by dropzone. The script is very simple. That works fine.<?php require_once($_SERVER['DOCUMENT_ROOT']."/test/site0/wp-load.php"); if ( ! function_exists( 'wp_handle_upload' ) ) { require_once( ABSPATH . 'wp-admin/includes/file.php' ); } if (!empty($_FILES)){ $uploadedfile = $_FILES['file']; $upload_overrides = array( 'test_form' => false ); $movefile = wp_handle_upload( $uploadedfile, $upload_overrides ); //put in wordpress default directory if ( $movefile && !isset( $movefile['error'] ) ) { imsanity_handle_upload($movefile); //plugin to resize image echo $movefile['url']; //response to front-side script } else { /** * Error generated by _wp_handle_upload() * @see _wp_handle_upload() in wp-admin/includes/file.php */ echo $movefile['error']; } } ?>
In front-end, a input field holds the images urls returned by script above, separated by commas.
When the formulary is submitted, those urls are processed by script I talked in first message. And that not work. Process the image, set the thumbnail but dont generate metadata.In another version, I process just one image send directly to file in form action. This one works, but i need the first one.
$cont = 1; foreach ($_FILES['my_image_upload']['tmp_name'] as $key) { $title = ereg_replace("[^a-zA-Z0-9_]", "", strtr($_POST["vehicle_title"], "áàãâéêíóôõúüçÁÀÃÂÉÊÍÓÔÕÚÜÇ ", "aaaaeeiooouucAAAAEEIOOOUUC_")); $content = file_get_contents($_FILES['my_image_upload']['tmp_name'][$key]); $ext = strtolower(substr($_FILES['my_image_upload']['name'][$key],-4)); $title = substr($title,0,30); //short the title 30 chars $title = $title . ++$cont . $ext; //adicionar a extensão $upload_dir = wp_upload_dir(); $aux = date('Y-m-d')."-".$title; //final title //add date to title $namae = $upload_dir['path']."/".$aux; //mount path $file = fopen($namae,'x'); fwrite($file,$content); fclose($file); //parameters for plugin Imsanity, thats resize image $params['type'] = image_type_to_mime_type(exif_imagetype($namae)); $params['file'] = $namae; $params['url'] = $upload_dir['url']."/".$aux; imsanity_handle_upload($params); $attachment = array( 'guid' => $params['url'], 'post_mime_type' => $params['type'], 'post_title' => sanitize_file_name($aux), 'post_content' => '', 'post_status' => 'inherit' ); $attach_id = wp_insert_attachment( $attachment, $params['file'], $pid ); //generate metadata require_once( ABSPATH . 'wp-admin/includes/image.php' ); $attach_data = wp_generate_attachment_metadata( $attach_id, $params['file'] ); wp_update_attachment_metadata( $attach_id, $attach_data ); set_post_thumbnail( $pid, $attach_id ); }//END FOR EACH
Be sure
$uploadedfile
is properly structured as$_FILES
for multiple files. It’s quite common for this to be wrong, causingwp_handle_upload()
to fail silently. The array structure that comes in from a typical multiple file HTML form field is typically wrong. All the names are in one sub-array, all the types in another, etc. Each sub-array should be the name, type, etc. for one file.
http://php.net/manual/en/reserved.variables.files.phpThanks until now bcworkz.
It is unlikely to be this. The dropbox backend script that I informed, works in both versions of codes.
I will verify again, but this script works fine.
I check de default upload directory and the register in the wp_post table. THe image was correclty registered in WP.I saw in somewere that wp_generate_attachment_metadata searches the mime type through the attach_id. Well, is all right in wp_post, so the image was correctly register in wp. I still dont understand why wp_generate_attachment_metadata works in one script and no at in another.
I will keep investigating.
- The topic ‘wp_generate_attachment_metadata returns empty array’ is closed to new replies.