Just migrated an old site to WordPress. This old website contain more than 1200 images and if i migrate manually it will take a long time. So i wrote two script, first to extract all the images and it’s description from the old website to a file. And second script is a WordPress plugin script to upload the image to it’s page and set the image caption (useful for gallery and lightbox description) according to the old website description automatically.
How to upload image using script in WordPress
Advertisements
- Here’s my sample code for upload image using script in WordPress.
12345678910111213141516171819202122232425262728293031323334// i'm reading a file that with the pattern below:-// Post_ID will be at line 1// Image URL 1||image description 1// Image URL 2||image description 2// ...$data = file_get_contents(plugin_dir_path(__FILE__) . '/mydatafile.txt');$lines = explode("\r", $data);$post_id = '';// loop thru all the lines in the fileforeach($lines as $i => $line) :// set the post_id if it's first lineif($i == 0) {$post_id = (int) $line;continue;}$tmp = explode('||', $line);$file_array['name'] = $tmp[1];$file_array['tmp_name'] = $tmp[0];$attachment_id = media_handle_sideload( $file_array, $post_id, $desc );// If error in storing the imageif ( is_wp_error($attachment_id) ) {return $attachment_id;}// Once upload done, we set the attachment caption as the image description$new_caption = array('ID' => $attachment_id, 'post_excerpt' => $desc);wp_update_post( $new_caption );endforeach;
Related posts:
N900: How to connect Wifi protected setup compliant Network
How to hide apache2 version number in error page
MySQL 5.5 PDO::__construct(): The server requested authentication method unknown to the client [mysq...
Symfony 1.4 - Customize Admin Generator listing to show data from foreign tables
Symfony 1.4: How to sort foreign key record?
Symfony Filter: Change ForeignKey drop down to Text field
WordPress: How to create left sidebar template in TwentyEleven theme?
Install eAccelerator to Optimize PHP performance
Share this with your friends:-