Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am currently writing phpNuke import Wrdpress plug-in. I have a problem with importing posts with images: + I already had written a routine which extracts image tag text and images from source post HTML - I need to replace img tags with the ones imported to wordpress media library.

Example:

phpNuke post content:

 ...bla bla... 
 <img src="http://external.site.com/images/image321.jpg"> 
 bla bla...

should be translated to wordpress into something like this:

 ...bla bla... 
 <img src="http://mysite.com/wp-content/uploads/506_large-127x130.jpg"> 
 bla bla...

I had found a wordpress function media_sideload_image which according to description imports image from given URL into a library and attaches it to the post with given id... But to replace image tag with new I need to:

  1. insert post with wp_insert_post and save post id
  2. use media_sideload_image to import and receive image tag
  3. extract src attribute from img tag
  4. replace src in post html
  5. update post with wp_update_post

This leads to duplicating post data (adding extra revision).

My questions are:

  • Is there any way to add image to Wordpress post in more straightforward way? (without double posting)
  • Is the linkage of image to post obligatory?
share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

My first suggestion is to output a WXR formatted file from phpNuke if possible. I'm not familiar with it, however I'm pretty sure it's possible on any platform with database access. If you aren't familiar with WXR, install a sandbox WP environment, create a few posts, categorize them, create and assign parent pages, upload image attachments, and then create an export under Tools -> Export. This file will give you all the syntax you need to use the standard Wordpress Importer plugin.

If that is not an option, I would just import all the posts initially using the method you've created, and then build out a function to migrate the images separately. It would make for a cleaner operation in my humble opinion.

For the cleanup script, I would simply build out my own function based on media_sideload_image since it does not return the desired attachment ID that you need.

function foo_import_image($url, $post_id, $desc, $set_as_featured = true){

    $tmp = download_url($url);
    preg_match('/[^\?]+\.(jpg|JPG|jpe|JPE|jpeg|JPEG|gif|GIF|png|PNG)/', $url, $matches);
    $file_array['name'] = basename($matches[0]);
    $file_array['tmp_name'] = $tmp;

    if ( is_wp_error( $tmp ) ) {
        @unlink($file_array['tmp_name']);
        $file_array['tmp_name'] = '';
    }

    $id = media_handle_sideload( $file_array, $post_id, $desc );

    if($set_as_featured)
        update_post_meta($post_id, '_post_thumbnail', $id);

    return $id;
}

Here's how I would implement the preceding function:

//do a query to get the post_content fields
//use_preg_match all to identify if the content contains an image

$old_url = 'http://foo.com/images/bar.jpg'; //this would be matched content
$description = 'Foo Bar Image';
$id = foo_import_image($old_url, $post_id, $description, false);
$new_url = wp_get_attachment_url($id)

global $wpdb;
$wpdb->query("UPDATE $wpdb->posts SET post_content = replace(post_content, '$old_url', '$new_url') WHERE ID = '$post_id' LIMIT 1");

My final suggestion is to create a separate dev environment to test this so as not to mess up a good import batch. Whenever I do this type of operation, I do a full DB dump before I start importing masses of images into the DB.

share|improve this answer
    
Thank you! Finally tested it - works like a charm! I had resolved double posting issue by updating post_contents in wp_posts by direct query to database. –  Yuriy Petrovskiy Jan 22 '12 at 21:49
add comment
$link = isset($attr['link']) && 'file' == $attr['link'] 
    ? wp_get_attachment_link($id, $size, false, false) 
    : wp_get_attachment_link($id, $size, true, false);
$link = str_replace(
    wp_get_attachment_image_src($id, 'full', false),                          
    wp_get_attachment_image_src($id, 'large', false), 
    $link
);
share|improve this answer
1  
Can you provide an explanation of what you've done in human terms? –  s_ha_dum Nov 29 '12 at 6:14
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.