Attaching Pictures to Posts / Pages Uploaded by CSV
-
I modified this plugin with just a little bit of code that allows you to attach a featured image to a post and multiple other images as well if you have many. It’s saved me a lot of time and trouble so I just wanted to share it. Someone more experienced should probably make sure it’s perfectly secure, but it uses all WordPress API functions so there shouldn’t be any glaring vulnerabilities.
BEFORE YOU UPLOAD THE CSV, YOU MUST UPLOAD THE IMAGES TO THE WORDPRESS MEDIA GALLERY FIRST. This is because we just find the image from it’s name in the wordpress database and properly attach it to the post you are creating from the csv upload.
To upload a featured image, make sure your theme supports this feature, then use the column heading:
_wp_attached_file
– the cell value should be the full file name such as example.jpg or example2.gifTo upload multiple images to a post for use in a gallery or just to have them all attached use the heading:
_wp_attached_files
– the cell value should be a comma separated list of full file names such as example.jpg, example2.jpeg, example3.gif, example4.pngTo get this to work you’ll need to edit the csv_importer.php file in the plugin directory so that the following function is added:
function get_ID_by_keyvalue ($csv_key, $csv_value){ $prefix_mo_year = date('Y/m/'); $value = $prefix_mo_year.$csv_value; $args = array( 'post_type' => 'attachment', 'post_status' => 'inherit', 'meta_key' => $csv_key, 'meta_value' => $value ); // Query to Search for Attachment ID based on file name $the_query = new WP_Query( $args ); // Get Us the Name if ( $the_query->have_posts() ) { while ( $the_query->have_posts() ) { $the_query->the_post(); $imageID = $the_query->post->ID; } } else { // no posts found } /* Restore original Post Data */ wp_reset_postdata(); return $imageID; }
And you’ll need to edit the existing
create_custom_fields
function to look like this:function create_custom_fields($post_id, $data) { foreach ($data as $k => $v) { // anything that doesn't start with csv_ is a custom field if (!preg_match('/^csv_/', $k) && $v != '') { if ($k=='_wp_attached_file') { $imageID = $this->get_ID_by_keyvalue('_wp_attached_file', $v); $my_post = array( 'ID' => $imageID, 'post_parent' => $post_id ); wp_update_post( $my_post ); add_post_meta($imageID, '_wp_attachment_image_alt', trim($data['csv_post_title']).' '.trim($data['csv_post_excerpt'])); add_post_meta($post_id, '_thumbnail_id', $imageID); //$this->log['notice'][] = "<b>Value is: {$v} for {$k}</b>"; } elseif ($k=='_wp_attached_files'){ $image_names = explode(", ", $v); foreach ($image_names as $image_name){ $this->log['notice'][] = "<b>Trying to add {$image_name} as an attached image.</b>"; $imageID = $this->get_ID_by_keyvalue( '_wp_attached_file', $image_name ); $my_post = array( 'ID' => $imageID, 'post_parent' => $post_id ); wp_update_post( $my_post ); } } else { add_post_meta($post_id, $k, $v); } } } }
As it’s currently written, this only works with upload structures that are sorted into year and month based folders and it will only work if you do your csv upload on the same month and year that the image was originally uploaded. I’m sure there’s a better way to do this, but I’m not aware of it at this time.
Good luck all!
- The topic ‘Attaching Pictures to Posts / Pages Uploaded by CSV’ is closed to new replies.