I'm currently using the
set_post_thumbnail() function to insert a programmatically uploaded image to a post.
The problem is that the function works perfectly when the user is logged in only.
If the user is not logged in, in the back-end, wordpress says that the file is attached to the post but does not display it as a featured image. I don't have any conditional statement in that function.
Does anybody have had the same issue? Is that a normal behavior?
Edit: Noticed that the issue showed up since I've upgraded Wordpress from 3.5.1 to 3.5.2 but I really don't know if it's related
Edit: Here is a sample of my code
// This part is for testing purpose, the file is well placed with the right permission $dir = '/home/web/tmpdir'; $data = $_POST['imageCanvas']; $preview = base64_decode( str_replace( 'data:image/png;base64,', '', $data ) ); $file = '000011.png'; $filename = $dir . '/' . $file; file_put_contents( $filename, $preview ); // Here I copy the test file directly into the wordpress uploads dir // After this line I find the file in the uploads dir, so, let's say that for now it works as intended $wp_filetype = wp_check_filetype( basename( $file ), null ); copy( $filename, '/home/web/wordpress/wp-content/uploads/' . basename( $file ) ); $attachment = array( "post_mime_type" => $wp_filetype['type'], "post_title" => preg_replace( '/\.[^.]+$/', '', basename( $file ) ), "post_content" => '', "post_status" => 'inherit' ); // This part worked well until I recently upgraded WP to 3.5.2... $attach_id = wp_insert_attachment( $attachment, $file, $lastpostID ); require_once( ABSPATH . 'wp-admin/includes/image.php' ); $attach_data = wp_generate_attachment_metadata( $attach_id, $file ); wp_update_attachment_metadata( $attach_id, $attach_data ); set_post_thumbnail( $lastpostID, $attach_id ); // This part returns the right ids in Apache logs... error_log( 'attachid: ' . $attach_id . ' - postid: ' . $lastpostID );
I can find the file attached to his post into the wordpress media library:
But if I open the post, I don't see any featured image.