• Resolved alkafy

    (@alkafy)


    I need to import several hundred images from folders already on the server and attach them to posts. As such, I’ve written a quick script (and borrowed pieces of some plugins) to test the procedure with one image.

    After upload, ‘Full size’ reads 0x0 pixels and ‘Thumbnail’ and ‘Medium’ are not configured. I’m having trouble finding any helpful documentation on the functions I need to use. Here’s my script:

    $file='wm4hd.jpg';
    $filepath=$_SERVER['document_root'].'/wp-content/uploads/2009/04/'.$file;
    
    // find TYPE
    $wp_filetype = wp_check_filetype(basename($file), null );
    extract($wp_filetype);
    if (!$type) $type = "";
    
    $title = 'wm4hd';
    $content = '';
    
    $url = 'https://www.example.com/wp-content/uploads/2009/04/'.$file;
    $file = array(
    	'file' => $filepath,
    	'url' => $url,
    	'type' => $type);
    $attachment = array(
    	'guid' => $url,
    	'post_mime_type' => $type,
    	'post_title' => $title,
    	'post_content' => $content,
    	'post_type' => 'attachment',
    	'post_parent' => 288
    	);
    
    $id = wp_insert_attachment($attachment, $filepath, 288);
    if (!is_wp_error($id)) {
    	wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $filepath));
    	echo "Imported: $filepath ($title)<br />";
    }
Viewing 7 replies - 1 through 7 (of 7 total)
  • I’ve only ever done this once successfully – via .csv – Here is a link to a script that I used: https://mfields.org/_plugins/mf-csv-import.zip

    I saved the file to the directory where WordPress is installed and ran it through the browser. This script is VERY resource intensive and I was only able to import about 30 records at a time.

    Please note that I’m am only posting this as a reference, the file may or may not work for you, but I hope that it does ??

    Thread Starter alkafy

    (@alkafy)

    Thank you, I’ll look into that file this afternoon. It’s good to hear I’m not alone in trying this ??

    No problem… I really wish something like this was built into WordPress, but I wish there were a lot of things built into WordPress…. Any way, let me know if you have any problems with this…

    Thread Starter alkafy

    (@alkafy)

    I got this working, albeit differently.. (but thank you for mfields – pouring over your code helped.)

    I embedded SWFUpload into a plugin admin page and used an ajax call to bulk upload the pictures that were formerly on my server and attach them. I figured out that my main problem was using an incorrect path for the $filepath variable.

    The shortened version using Post ID 288 as a test:

    $save_path = “../../uploads/”;
    $file_name = preg_replace(‘/[^’.$valid_chars_regex.’]|\.+$/i’, “”, basename($_FILES[$upload_name][‘name’]));

    move_uploaded_file($_FILES[$upload_name][“tmp_name”], $save_path.$file_name;

    $url = get_bloginfo(‘url’).’/wp-content/uploads/’.$file_name;
    $file = realpath($save_path).’/’.$file_name;

    $attachment = array(
    ‘post_mime_type’ => ‘image/jpeg’,
    ‘guid’ => $url,
    ‘post_parent’ => 288,
    ‘post_title’ => preg_replace(‘/\.[^.]+$/’, ”, basename($file_name)),
    ‘post_content’ => ‘file: ‘.$file.’ url:’.$url
    );

    $id = wp_insert_attachment($attachment, $file, 288);
    wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));

    Thread Starter alkafy

    (@alkafy)

    Footnote: What I meant to say – turning it into an SWFUpload made me realize my mistake of using an incorrect path. Knowing that, it’s probably not too hard to modify to use existing server files. The function realpath() saved me, but simply designating an absolute path to start should work.

    Thread Starter alkafy

    (@alkafy)

    Modded for existing server files instead of upload. Limited to 7 for testing. Hope someone else finds it useful.

    File structure is: /uploads/import/########/image.jpg
    ######## is a secret custom field on each post (_origID)

    $path = realpath(‘../wp-content/uploads/import’);
    $dirs = array_diff(scandir($path),array(‘.’,’..’));

    $count=0;

    foreach ($dirs as $ad_number) {
    if (is_dir($path.’/’.$ad_number)) {
    $ad_image_path = $path.’/’.$ad_number;
    $ad_images = array_diff(scandir($ad_image_path),array(‘.’,’..’));

    query_posts(‘showposts=1&meta_key=_origID&meta_value=’.$ad_number);

    while (have_posts()) : the_post();

    foreach ($ad_images as $ad_image) {
    $count++;
    $extension = end(explode(‘.’,$ad_image));
    if (strtolower($extension)==’jpg’ && $count<=7) {

    $url = get_bloginfo(‘url’).’/wp-content/uploads/import/’.$ad_number.’/’.$ad_image;
    $file = $ad_image_path.’/’.$ad_image;
    $post_id = get_the_ID();

    // Construct the attachment array
    $attachment = array(
    ‘post_mime_type’ => ‘image/jpeg’,
    ‘guid’ => $url,
    ‘post_parent’ => $post_id,
    ‘post_title’ => preg_replace(‘/\.[^.]+$/’, ”, basename($ad_image)),
    ‘post_content’ => ‘image…’
    );

    // Save the data

    $id = wp_insert_attachment($attachment, $file, $post_id);
    wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));

    }
    }

    endwhile;
    }
    }

    This sounds awesome!!! Thanks for posting your fix… I’m totally gonna check this out soon as I need to completely need to rework my portfolio.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘How to manually generate thumbnails?’ is closed to new replies.