Skip to content

Instantly share code, notes, and snippets.

@kaioken
Last active September 9, 2019 02:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kaioken/1e0d1af80ea6fab8f37f6720b8a0f3f9 to your computer and use it in GitHub Desktop.
Save kaioken/1e0d1af80ea6fab8f37f6720b8a0f3f9 to your computer and use it in GitHub Desktop.
Wordpress Post Migration
<?php
/**
* Wordpress migration from another wordpress site via its API
*/
$path = '/home/websitefolder/public';
$wordpressUrl = 'https://migratingwordpresssite.com';
$redisHost = '127.0.0.1';
require $path.'/wp-load.php';
/**
* Get the post of the WP site to migrate.
*
* @return void
*/
function getPost(string $url)
{
$pages = 100;
for ($i = 1; $i < $pages; $i++) {
$posts = file_get_contents($url . $i . '&_embed');
$posts = json_decode($posts);
yield $posts;
}
}
$redis = new Redis();
$redis->connect($redisHost, 6379);
$archivoUrl = $wordpressUrl.'/wp-json/wp/v2/posts?page=';
foreach (getPost($archivoUrl) as $posts) {
foreach ($posts as $post) {
$slug = $post->slug;
//ok we have already inserted this , continue
if ($redis->get($slug)) {
continue;
}
$embbed = 'wp:featuredmedia';
if (isset($post->_embedded->$embbed[0]->source_url)) {
$imageUrl = $post->_embedded->$embbed[0]->source_url;
}
$post = [
'post_content' => html_entity_decode($post->content->rendered), // The full text of the post.
'post_name' => $slug, // The name (slug) for your post
'post_title' => html_entity_decode($post->title->rendered), // The title of your post.
'post_status' => $post->status, // Default 'draft'.
'post_type' => $post->type, // Default 'post'.
'post_author' => $post->author, // The user ID number of the author. Default is the current user ID.
'ping_status' => $post->ping_status, // Pingbacks or trackbacks allowed. Default is the option 'default_ping_status'.
'post_excerpt' => $post->excerpt->rendered, // For all your post excerpt needs.
'post_date' => $post->date, // The time post was made.
'post_date_gmt' => $post->date_gmt, // The time post was made, in GMT.
'comment_status' => $post->comment_status, // Default is the option 'default_comment_status', or 'closed'.
'tags_input' => $post->tags, // Default empty.
'post_category' => $post->categories, // Default empty.
];
//save to WP + Image
$postId = wp_insert_post($post);
if ($imageUrl) {
if ($imageContent = @file_get_contents($imageUrl)) {
$imageName = basename($imageUrl);
file_put_contents($imageName, $imageContent);
}
}
$wp_filetype = wp_check_filetype($imageName, null);
if ($imageName) {
$attachment_data = [
'post_mime_type' => $wp_filetype['type'],
'post_title' => sanitize_file_name($imageName),
'post_content' => '',
'post_status' => 'inherit'
];
$attach_id = wp_insert_attachment($attachment_data, $imageName, $postId);
}
if ($postId) {
$redis->set($slug, 1);
}
echo "Post Migrated {$slug} - ID : $postId ".PHP_EOL;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment