Skip to content

Instantly share code, notes, and snippets.

@billjohnston
Created February 22, 2013 17:46
Show Gist options
  • Save billjohnston/5015241 to your computer and use it in GitHub Desktop.
Save billjohnston/5015241 to your computer and use it in GitHub Desktop.
Transfer wordpress blogs, reupload images, add image as 'featured image' for advanced custom fields. Edit switch statement to fix categories. -Place each file in the respective Wordpress root. -Run transferOld.php in browser and copy the output. -Paste output into transferNew.php and run in browser. -Change offset in transferOld.php and repeat t…
<?php
/////////////////paste wpArray here/////////////////
/////////////////paste wpArray here/////////////////
require_once('wp-load.php' );
global $wpdb;
$count = 1;
foreach($wpArray as $key=>$value){
$my_post = array(
'post_title' => $wpArray[$key]['title'],
'post_date' => $wpArray[$key]['date'],
'post_content' => $wpArray[$key]['content'],
'post_author' => 2,
'post_status' => 'publish',
'post_category' => $wpArray[$key]['categories']
);
$the_post_id = wp_insert_post( $my_post );
$upload_dir = wp_upload_dir();
$image_data = file_get_contents($wpArray[$key]['image']);
$filename = basename($wpArray[$key]['image']);
if(wp_mkdir_p($upload_dir['path']))
$file = $upload_dir['path'] . '/' . $filename;
else
$file = $upload_dir['basedir'] . '/' . $filename;
file_put_contents($file, $image_data);
$wp_filetype = wp_check_filetype($filename, null );
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => sanitize_file_name($filename),
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment( $attachment, $file, $post_id );
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( $the_post_id, $attach_id );
update_post_meta($the_post_id, 'featured_image', $attach_id);
echo $count."<br/>";
}
/////////////////////
function imgSrc ($string){
if(strpos($string,'<img') !== false){
$imgStart = strpos($string,"<img");
$srcStart = strpos($string,'src="',$imgStart)+5;
$srcEnd = strpos($string,'"',$srcStart);
$srcLength = $srcEnd-$srcStart;
return substr($string,$srcStart,$srcLength);
}
else{
return '';
}
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
</head>
<body>
<textarea cols="100" rows="100">$wpArray=array();
<?php
require_once('wp-load.php' );
global $wpdb;
global $post;
$count=0;
$args = array('numberposts' => '500','orderby' => 'post_date', 'order' => 'ASC', 'offset' => 0);//increase offset by 500
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post); ?>
<?php
$category = get_the_category();
$tags = get_the_tags();
if(is_array($tags)){
$both = array_merge($tags, $category);
}
else{
$both = $category;
}
$title = str_replace('"','\"',get_the_title());
$content = preg_replace('~<a .*?>\s*<img .*?>\s*</a>~i', '', get_the_content());
$replaceArray = array('http://maxskansascity.com','http://www.maxskansascity.com');
$content = str_replace($replaceArray,"http://mkc.inc.gs",$content);
$content = str_replace('"','\"',$content);
$img = imgSrc(get_the_content());
$date = get_the_time("Y-m-d H:i:s");
$catFinal = array();
foreach ($both as $key => $value){
$catFinal[] = categorySwitch($both[$key]->term_id);
}
$catFinal = array_unique($catFinal);
if (count($catFinal)>1){$catFinal= array_diff($catFinal,array(7));}
if (count($catFinal)==0){$catFinal[]=7;}
$catFinal = array_values($catFinal);
?>
$wpArray[<?php echo $count;?>]=array("title"=>"<?php echo $title?>", "content"=>"<?php echo $content;?>", "image"=>"<?php echo $img;?>","date"=>"<?php echo $date;?>","categories"=>array(<?php foreach($catFinal as $key => $value){echo '"'.$value.'"'; if($key < count($catFinal)-1){echo ',';};}?>));
<?php $count++; endforeach; ?>
</textarea>
</body>
</html>
<?php
function imgSrc ($string){
if(strpos($string,'<img') !== false){
$imgStart = strpos($string,"<img");
$srcStart = strpos($string,'src="',$imgStart)+5;
$srcEnd = strpos($string,'"',$srcStart);
$srcLength = $srcEnd-$srcStart;
return substr($string,$srcStart,$srcLength);
}
else{
return '';
}
}
function categorySwitch($theCategory){
switch($theCategory){
case 44:
$answer = 1;
break;
case 43:
$answer = 2;
break;
case 777:
$answer = 2;
break;
case 2399:
$answer = 6;
break;
case 2400:
$answer = 6;
break;
case 2401:
$answer = 6;
break;
case 42:
$answer = 3;
break;
case 757:
$answer = 3;
break;
case 41:
$answer = 4;
break;
case 775:
$answer = 4;
break;
case 61:
$answer = 5;
break;
default:
$answer = 7;
break;
}
return $answer;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment