Or, embed this snippet using GenerateWP WordPress Plugin.

Download

Clone

Snippet Importer

Snippet Importer

<?php
/*
Plugin Name: Snippet Importer
Plugin URI: http://stephanieleary.com/
Description: Import an arbitrary HTML snippet into a post.
Author: sillybean
Author URI: http://stephanieleary.com/
Version: 0.1
Text Domain: import-snippet
License: GPL version 2 or later - http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
*/

if ( ! defined( 'WP_LOAD_IMPORTERS' ) )
	return;

/** Display verbose errors */
define( 'IMPORT_DEBUG', false );

// Load Importer API
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;
}

if ( class_exists( 'WP_Importer' ) ) {
class Snippet_Importer extends WP_Importer {

	var $posts = array();
	var $snippet = '';
	
	function Snippet_Importer() {}

	function header() {
		echo '<div class="wrap">';
		screen_icon();
		echo '<h2>'.__( 'Snippet Importer', 'import-snippet' ).'</h2>';
	}

	function footer() {
		echo '</div>';
	}

	function dispatch() {
		if ( empty ( $_GET['step'] ) )
			$step = 0;
		else
			$step = ( int ) $_GET['step'];

		$this->header();

		switch ( $step ) {
			case 0 :
				$this->greet();
				break;
			case 1 :
				check_admin_referer( 'snippet-import' );
				$result = $this->import();
				if ( is_wp_error( $result ) )
					echo $result->get_error_message();
				break;
		}

		$this->footer();
	}
	
	function greet() { ?>
		
		<h4><?php _e( 'Paste your HTML snippet below.' ); ?></h4>
		<form method="post" action="admin.php?import=snippet&amp;step=1">
		<p>
			<textarea name="snippet-text" class="widefat"></textarea>
		</p>
		
		<input type="hidden" name="action" value="save" />
		
		<p class="submit">
			<input type="submit" name="submit" class="button" value="<?php echo esc_attr( __( 'Import', 'import-snippet' ) ); ?>" />
		</p>
		<?php wp_nonce_field( 'snippet-import' ); ?>
		</form>
		</div>
	<?php
	}
	
	function import() {
		echo '<h2>'.__( 'Importing HTML snippet...', 'import-snippet' ).'</h2>';
		$this->snippet = $_POST['snippet-text'];
		$this->get_post();
		do_action( 'import_done', 'snippet' );
	}
	
	function get_post() {

		$xml = new SimpleXMLElement( $this->snippet );
		// avoid asXML errors when it encounters character range issues
		libxml_clear_errors();
		libxml_use_internal_errors( false );
		
		// start building the WP post object to insert
		$my_post = array();
		
		// post type
		$my_post['post_type'] = 'post';

		// title
		
		// content
		$my_post['post_content'] = '';
		
		// custom fields
		
		// taxonomies
				
		$post_id = wp_insert_post( $my_post );
		
		// handle errors
		if ( is_wp_error( $post_id ) )
			   printf( '<div id="message" class="error"><p>%s</p></div>', $post_id->get_error_message() );
		
		// if no errors, handle custom fields
		// add_post_meta( $post_id, $fieldname, $fieldvalue, true );
				
		// ... and all the taxonomies...
		// wp_set_post_terms( $post_id, $tags, 'post_tag', false );
		
		// show success
		printf( __( 'Imported the snippet as %s.', 'import-snippet' ), '<a href="post.php?action=edit&post='.$post_id.'">'.$my_post['post_title'].'</a>' );
		}
	}  // get_post()
	

} // class_exists( 'WP_Importer' )

global $snippet_importer;
$snippet_importer = new Snippet_Importer();

register_importer( 'snippet', __( 'Snippet', 'import-snippet' ), __( 'Import an HTML snippet.', 'import-snippet' ), array ( $snippet_importer, 'dispatch' ) );