I have seen answers to a similar issue but they didn't help resolve the problem. Any help is greatly appreciated. Thanks!
Problem line:
$postvals['attachment'][$i] = array( 'post_title' => $renamed,'post_content' => '','post_excerpt' => '','post_mime_type' => $file['type'],'guid' => $file['url'], 'file' => $file['file'] );
Complete code:
function cp_process_new_image() {
global $wpdb;
$postvals = '';
for ( $i=0; $i < count( $_FILES['image']['tmp_name'] ); $i++ ) {
if ( !empty($_FILES['image']['tmp_name'][$i]) ) {
// rename the image to a random number to prevent junk image names from coming in
$renamed = mt_rand( 1000,1000000 ).".".mastheme_find_ext( $_FILES['image']['name'][$i] );
//Because WP can't handle multiple uploads as of 2.8.5
$upload = array( 'name' => $renamed,'type' => $_FILES['image']['type'][$i],'tmp_name' => $_FILES['image']['tmp_name'][$i],'error' => $_FILES['image']['error'][$i],'size' => $_FILES['image']['size'][$i] );
// need to set this in order to send to WP media
$overrides = array( 'test_form' => false );
// check and make sure the image has a valid extension and then upload it
$file = cp_image_upload( $upload );
if ( $file ) // put all these keys into an array and session so we can associate the image to the post after generating the post id
$postvals['attachment'][$i] = array( 'post_title' => $renamed,'post_content' => '','post_excerpt' => '','post_mime_type' => $file['type'],'guid' => $file['url'], 'file' => $file['file'] );
}
}
return $postvals;
}
“Cannot use string offset as an array” Fatal Error
As shown in your code, $postvals
is a string and not an array, so the key for attachment does not exist.
Changing:
$postvals = '';
To one of the following:
$postvals = array();
$postvals = [];
Will initialize the varaible as the correct type.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.