Skip to content

Instantly share code, notes, and snippets.

@RalfAlbert
Created February 9, 2013 04:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save RalfAlbert/4743800 to your computer and use it in GitHub Desktop.
Save RalfAlbert/4743800 to your computer and use it in GitHub Desktop.
A custoim importer for WordPress
<?php
/*
Plugin Name: Custom Importer
Plugin URI:
Description: Import posts or not
Author: Ralf Albert
Author URI: http://yoda.neun12.de
Version: 0.1
Text Domain: custom-importer
License: GPL version 2 or later - http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
*/
namespace RalfAlbert\CustomImporter;
// check if we come from admin.php?import=...
if( ! defined( 'WP_LOAD_IMPORTERS' ) )
return;
// Load Importer API (Importer API = Backendpage admin.php?import=...)
require_once ABSPATH . 'wp-admin/includes/import.php';
if ( ! class_exists( 'WP_Importer' ) ) {
$class_wp_importer = ABSPATH . 'wp-admin/includes/class-wp-importer.php';
if ( file_exists( $class_wp_importer ) )
require $class_wp_importer;
}
function custom_importer_init() {
load_plugin_textdomain( 'custom-importer', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
register_importer(
'custom',
'CustomPress',
__('Import <strong>posts</strong> or not.', 'custom-importer'),
array(
new Custom_Import(),
'dispatch'
)
);
}
add_action( 'admin_init', __NAMESPACE__ . '\custom_importer_init' );
class Custom_Import
{
public function dispatch() {
$this->header();
$step = filter_input( INPUT_GET, 'step', FILTER_SANITIZE_NUMBER_INT );
if( empty( $step ) )
$step = 0;
switch( $step ) {
// start import
case 0:
default:
$this->greet();
$this->enter_url();
break;
case 1:
$this->read_xml();
$this->parse_xml();
$this->sanitize_data();
$this->insert_data();
break;
case 2:
$this->finish();
break;
}
$this->footer();
}
// Display import page title
public function header() {
echo '<div class="wrap">';
screen_icon();
echo '<h2>' . __( 'Import CustomPress', 'custom-importer' ) . '</h2>';
}
// Close div.wrap
public function footer() {
echo '</div>';
}
/**
* Display introductory text and blah
*/
public function greet() {
echo '<div class="narrow">';
echo '<p>'.__( 'Howdy!', 'custom-importer' ).'</p>';
echo '</div>';
}
public function enter_url() {
echo 'Display input form for url';
/*
* Displaying a input form to enter an url
* The form data should be send to the current page and
* an additional query argument 'action=1' to anter the next step
*/
$this->redirect( 1 );
return TRUE;
}
protected function read_xml() {
echo '<ol>';
echo '<li>Read XML...</li>';
}
protected function parse_xml() {
echo '<li>Parsing XML...</li>';
}
protected function sanitize_data() {
echo '<li>Sanitizing data...</li>';
}
protected function insert_data() {
echo '<li>Inserting data...</li>';
echo '</ol>';
$this->redirect( 2 );
}
protected function finish() {
echo '<p>All done!</p>';
}
protected function redirect( $step = 0 ) {
$url = home_url( add_query_arg( array( 'step' => $step) ) );
printf( '<p><a href="%s">Next step...</a></p>', $url );
return TRUE;
}
@franz-josef-kaiser
Copy link

Referenced on WordPress StackExchange.

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