Skip to content

Instantly share code, notes, and snippets.

@wgroenewold
Created March 3, 2016 08:35
Show Gist options
  • Save wgroenewold/7ab86224aa2e6c9f9268 to your computer and use it in GitHub Desktop.
Save wgroenewold/7ab86224aa2e6c9f9268 to your computer and use it in GitHub Desktop.
<?php
///*
//Plugin Name: MODX importer
//Plugin URI: http://gho.nl/
//Description: Import newsitems from MODX.
//Version: 1.0
//Author: Wouter Groenewold
//License: GPL
//Copyright: GH+O Communicatie
//*/
//
require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
function modx_import() {
if(!class_exists( 'Redirection_Admin' ) ) {
//Dependency check. Redirection not activated, bail.
deactivate_plugins( plugin_basename( __FILE__ ) );
wp_die( 'This plugin requires Redirection.' );
}else{
//configuration
$servername = "";
$table_prefix = '';
$redirection_group = 3; //make sure this is present!
//old server config
$username = "";
$password = "";
$db = "";
// Create connection
$conn = new mysqli($servername, $username, $password, $db);
$conn->set_charset('utf8mb4'); //set to extended charset so special chars get a good import.
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT pagetitle, alias, introtext, content, publishedon FROM modx_site_content";
$result = $conn->query($sql);
$data = array();
$term = get_term_by( 'slug', 'imported', 'category' );
$term_id = $term->term_id;
while($row = $result->fetch_assoc()) {
$epoch = $row['publishedon'];
$dt = new DateTime("@$epoch");
$date = $dt->format('Y-m-d H:i:s');
$data[] = array(
'title' => $row['pagetitle'],
'content' => $row['content'],
'post_date' => $date,
'post_name' => $row['alias'],
'intro' => $row['introtext']
);
}
foreach($data as $value){
$args = array(
'post_date' => $value['post_date'],
'post_content' => $value['content'],
'post_status' => 'publish',
'post_type' => 'nieuws',
'post_name' => $value['post_name'],
'post_title' => $value['title'],
'tax_input' => array( 'category' => $term_id )
);
$postid = wp_insert_post($args);
if($postid){
update_field('intro', $value['intro'], $postid);
$oldurl = '/nieuws/nieuws/' . $value['post_name'];
$newurl = get_permalink($postid);
global $wpdb;
$wpdb->insert(
$table_prefix . '_redirection_items',
array(
//id = autoincrement
'url' => $oldurl,
//regex = default 0
//position = default 0
'last_count' => 0,
'last_access' => '0000-00-00 00:00:00',
'group_id' => $redirection_group,
//status = default enabled
'action_type' => 'url',
'action_code' => 301,
'action_data' => $newurl,
'match_type' => 'url'
),
array(
//
"%s",
//
//
"%d",
"%s",
"%d",
//
"%s",
"%d",
"%s",
"%s"
)
);
}else{
echo 'Failed to import ' . $value['post_title'];
}
}
$conn->close();
deactivate_plugins( plugin_basename( __FILE__ ) );
wp_die( 'Finished importing' );
}
}
add_action( 'init', 'modx_import' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment