Created
February 28, 2019 02:12
-
-
Save dgroddick/0885106ef66a66a916bd422a8d18a7b1 to your computer and use it in GitHub Desktop.
A simple PHP script that migrates Blog posts from Shopify to WordPress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
set_time_limit(0); | |
ignore_user_abort(1); | |
error_reporting(E_ALL & ~E_NOTICE); | |
ini_set('display_errors', 'on'); | |
require_once(dirname(__FILE__) . '/vendor/autoload.php'); | |
require_once(ABSPATH . 'wp-admin/includes/media.php'); | |
require_once(ABSPATH . 'wp-admin/includes/file.php'); | |
require_once(ABSPATH . 'wp-admin/includes/image.php'); | |
global $wpdb; | |
// API credentials | |
$config = array( | |
'ApiKey' => '', | |
'Password' => '', | |
'ShopUrl' => 'yoursite.myshopify.com' | |
); | |
$shopify = new PHPShopify\ShopifySDK($config); | |
// The Blog ID from Shopify | |
$blog_id = ''; | |
$blog = $shopify->Blog($blog_id)->Article->get(); | |
foreach($blog as $b) { | |
$post_id = wp_insert_post(array( | |
'post_title' => $b['title'], | |
'post_content' => $b['body_html'], | |
'post_status' => 'publish', | |
'post_date' => $b['published_at'], | |
'post_excerpt' => $b['summary_html'], | |
'post_author' => 1, | |
'tags_input' => $b['tags'] | |
)); | |
$cat = wp_set_post_categories($post_id, 2); | |
$media = media_sideload_image($b['image']['src'], $post_id); | |
if (!empty($media) && !is_wp_error($media)) { | |
$args = array( | |
'post_type' => 'attachment', | |
'posts_per_page' => -1, | |
'post_status' => 'any', | |
'post_parent' => $post_id | |
); | |
$attachments = get_posts($args); | |
if (isset($attachments) && is_array($attachments)) { | |
foreach ($attachments as $attachment) { | |
$image = wp_get_attachment_image_src($attachment->ID, 'full'); | |
if (strpos($media, $image[0]) !== false) { | |
set_post_thumbnail($post_id, $attachment->ID); | |
break; | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment