Skip to content

Instantly share code, notes, and snippets.

@janboddez
Created August 9, 2021 10:31
Show Gist options
  • Save janboddez/4b9cfcd9d3d6038b024c84557e1705cd to your computer and use it in GitHub Desktop.
Save janboddez/4b9cfcd9d3d6038b024c84557e1705cd to your computer and use it in GitHub Desktop.
<?php
/**
* Quick and dirty way to import a Twitter export into WordPress
*/
define('WP_USE_THEMES', false);
require './wordpress/wp-load.php';
$username = '<your-username>';
$tweets = file_get_contents('tweet.js');
$tweets = json_decode($tweets, true);
foreach ($tweets as $tweet) {
$datetime = DateTime::createFromFormat('D M d H:i:s P Y', $tweet['tweet']['created_at']);
$datetime = $datetime->format('Y-m-d H:i:s');
$post_id = wp_insert_post([
'post_status' => 'draft',
'post_type' => 'iwcpt_note',
'post_date_gmt' => $datetime,
'post_date' => get_date_from_gmt($datetime),
'post_title' => '',
'post_content' => $tweet['tweet']['full_text'],
'meta_input' => [
'_share_on_twitter_id' => $tweet['tweet']['id_str'],
'_share_on_twitter_url' => 'https://twitter.com/'.$username.'/status/'.$tweet['tweet']['id_str'],
],
]);
}
@janboddez
Copy link
Author

Should probably have added a check for in_reply_to_status_id_str and import these last and link them, if applicable, to previously imported Tweets, using a meta search and the u-in-reply-to microformat.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment