<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Nazgul on WordPress &#187; Tutorials</title>
	<atom:link href="http://wordpress.nazgul.nu/category/tutorials/feed/" rel="self" type="application/rss+xml" />
	<link>http://wordpress.nazgul.nu</link>
	<description>Bits about WordPress</description>
	<lastBuildDate>Sat, 29 Mar 2008 19:20:48 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9-rare</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>New in 2.3: Importer Plugins</title>
		<link>http://wordpress.nazgul.nu/2007/09/14/new-in-23-importer-plugins/</link>
		<comments>http://wordpress.nazgul.nu/2007/09/14/new-in-23-importer-plugins/#comments</comments>
		<pubDate>Fri, 14 Sep 2007 11:00:55 +0000</pubDate>
		<dc:creator>Nazgul</dc:creator>
				<category><![CDATA[Features & Bugs]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[2.3]]></category>
		<category><![CDATA[importer]]></category>
		<category><![CDATA[new feature]]></category>
		<category><![CDATA[plugin]]></category>

		<guid isPermaLink="false">http://wordpress.nazgul.nu/2007/09/14/new-in-23-importer-plugins/</guid>
		<description><![CDATA[Eventhough WordPress 2.3 hasn't yet been released to the general public at the moment I'm writing this, I'd like to do one or more posts on some of the new features that went into 2.3. Although tagging is propably the biggest new addition, everybody is already blogging about it, or will be. So I'll be [...]]]></description>
			<content:encoded><![CDATA[<!-- google_ad_section_start --><p>Eventhough WordPress 2.3 hasn't yet been released to the general public at the moment I'm writing this, I'd like to do one or more posts on some of the new features that went into 2.3. Although tagging is propably the biggest new addition, everybody is already blogging about it, or will be. So I'll be concentrating one some smaller, but still very usefull new features.</p>
<p>In this post, that will be Importer Plugins. What are Importer Plugins? As most of you know, WordPress comes with a set of importers which can be used to import data from other formats/blogs into your blog. (Think DotClear, Blogger and many more) But there are many more systems out there which don't have importers that are build into WordPress. That's where plugins come in. A plugin can be used to extend the default WordPress behaviour and starting with 2.3 can also be used to add your own importer to WordPress.</p>
<p>So how do you create such a Importer Plugin? Let's build one step by step. We start with an empty plugin template. (For basic information on writing plugins I recommend reading the Codex <a href="http://codex.wordpress.org/Writing_a_Plugin" class="liwp">Writing a Plugin</a> article or <a href="http://wpbits.wordpress.com/2007/06/18/writing-wordpress-plugin-widget-loader/" class="liexternal">this tutorial by Leonid Mamchenkov</a>)</p>
<pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #808080; font-style: italic;">/*
Plugin Name: Importer Plugin
Version: 0.1
Plugin URI: http://wordpress.nazgul.nu/
Description: Example of an importer plugin.
Author: Nazgul
Author URI: http://nazgul.nu/
*/</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/*  Copyright 2007 Nazgul
&nbsp;
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.
&nbsp;
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
&nbsp;
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre>
<p>So how do we turn this into an importer? We create a class which will do the importing for us. I based this class on the bundled importers, as they almost all use this form, but stripped out the fluff for now.</p>
<pre class="php"><span style="color: #808080; font-style: italic;">// The importer</span>
<span style="color: #000000; font-weight: bold;">class</span> Test_Import <span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0000ff;">$importer_code</span> = <span style="color: #ff0000;">'importerplugin'</span>;
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0000ff;">$importer_name</span> = <span style="color: #ff0000;">'Importer Plugin'</span>;
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0000ff;">$importer_desc</span> = <span style="color: #ff0000;">'Import stuff'</span>;
&nbsp;
	<span style="color: #808080; font-style: italic;">// Function that will handle the wizard-like behaviour</span>
	<span style="color: #000000; font-weight: bold;">function</span> dispatch<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><a href="http://www.php.net/empty" class="liexternal"><span style="color: #000066;">empty</span></a> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$_GET</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'step'</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
			<span style="color: #0000ff;">$step</span> = <span style="color: #cc66cc;">0</span>;
		<span style="color: #b1b100;">else</span>
			<span style="color: #0000ff;">$step</span> = <span style="color: #66cc66;">&#40;</span>int<span style="color: #66cc66;">&#41;</span> <span style="color: #0000ff;">$_GET</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'step'</span><span style="color: #66cc66;">&#93;</span>;
&nbsp;
		<span style="color: #b1b100;">switch</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$step</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">case</span> <span style="color: #cc66cc;">0</span> :
				wp_import_upload_form<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'admin.php?import='</span>.<span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">importer_code</span>.<span style="color: #ff0000;">'&amp;amp;step=1'</span><span style="color: #66cc66;">&#41;</span>;
				<span style="color: #b1b100;">break</span>;
			<span style="color: #b1b100;">case</span> <span style="color: #cc66cc;">1</span> :
				check_admin_referer<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'import-upload'</span><span style="color: #66cc66;">&#41;</span>;
				<span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">import</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
				<span style="color: #b1b100;">break</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">// Function that does the actual importing</span>
	<span style="color: #000000; font-weight: bold;">function</span> import<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #0000ff;">$file</span> = wp_import_handle_upload<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> <a href="http://www.php.net/isset" class="liexternal"><span style="color: #000066;">isset</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$file</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'error'</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
			<a href="http://www.php.net/echo" class="liexternal"><span style="color: #000066;">echo</span></a> <span style="color: #ff0000;">'&lt; p&gt;Sorry, there has been an error.&lt; /p&gt;'</span>;
			<a href="http://www.php.net/echo" class="liexternal"><span style="color: #000066;">echo</span></a> <span style="color: #ff0000;">'&lt; p&gt;&lt;strong&gt;'</span> . <span style="color: #0000ff;">$file</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'error'</span><span style="color: #66cc66;">&#93;</span> . <span style="color: #ff0000;">'&lt;/strong&gt;&lt; /p&gt;'</span>;
			<span style="color: #b1b100;">return</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">file</span> = <span style="color: #0000ff;">$file</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'file'</span><span style="color: #66cc66;">&#93;</span>;
		<span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">id</span> = <span style="color: #66cc66;">&#40;</span>int<span style="color: #66cc66;">&#41;</span> <span style="color: #0000ff;">$file</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'id'</span><span style="color: #66cc66;">&#93;</span>;
&nbsp;
		<span style="color: #808080; font-style: italic;">// TODO: Write import code</span>
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">// Default constructor</span>
	<span style="color: #000000; font-weight: bold;">function</span> Test_Import<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">// Nothing.</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre>
<p>What we see is a class with a constructor which does nothing, 2 functions and a couple of variables. Let's start with those variables. <em>importer_code</em> is the name which WordPress will be using to identify the importer internally. <em>importer_name</em> is the human readable name which will be shown on the Importer screen end <em>importer_desc</em> is the description which is displayed on that same page.</p>
<p>The importers are wizards. That means they can have multiple screens during various stages of the import process. That's where our dispatch function comes in. It shows the different screens based on the step in the wizard where you currently are. Currently the first step (0) shows an upload form and the second step does some security checking and runs the actual import by means of the <em>import</em> function.</p>
<p>So now that we have an importer, how do we let WordPress know it exists? We register it using the <em>register_importer</em> function. That function is part of <em>wp-admin/includes/import.php</em> which isn't loaded by default, so we'll have to do that as well.</p>
<pre class="php"><span style="color: #808080; font-style: italic;">// Instantiate and register the importer</span>
<span style="color: #b1b100;">include_once</span><span style="color: #66cc66;">&#40;</span>ABSPATH . <span style="color: #ff0000;">'wp-admin/includes/import.php'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><a href="http://www.php.net/function_exists" class="liexternal"><span style="color: #000066;">function_exists</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'register_importer'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
	<span style="color: #0000ff;">$test_import</span> = <span style="color: #000000; font-weight: bold;">new</span> Test_Import<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	register_importer<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$test_import</span>-&gt;<span style="color: #006600;">importer_code</span>, <span style="color: #0000ff;">$test_import</span>-&gt;<span style="color: #006600;">importer_name</span>,
		<span style="color: #0000ff;">$test_import</span>-&gt;<span style="color: #006600;">importer_desc</span>, <a href="http://www.php.net/array" class="liexternal"><span style="color: #000066;">array</span></a> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$test_import</span>, <span style="color: #ff0000;">'dispatch'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre>
<p>We use <em>include_once</em> and <em>function_exists</em> because we don't want to kill the blog if the inclusion fails for some reason. (It will on pre 2.3 version because the <em>import.php</em> was located elsewhere) Next we instantiate our importer and register it, passing in our plugin code, name, description and dispatch function. That's all there is to building an Importer Plugin. I admit that this plugin doesn't actually import anything, but I'll leave that up as an exercise for the reader. <img src='http://wordpress.nazgul.nu/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>One last thing I'd like to include is some sort of protection. As stated, this behaviour is new to 2.3 and this plugin therefore won't work on older versions. Let's make sure people who try this plugin on older versions can't accidentally activate this plugin.</p>
<pre class="php">add_action<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'activate_'</span>.plugin_basename<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">__FILE__</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #ff0000;">'importerplugin_activate'</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> importerplugin_activate<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
	<a href="http://www.php.net/global" class="liexternal"><span style="color: #000066;">global</span></a> <span style="color: #0000ff;">$wp_db_version</span>;
&nbsp;
	<span style="color: #808080; font-style: italic;">// Deactivate on pre 2.3 blogs</span>
	<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$wp_db_version</span>&lt;<span style="color: #cc66cc;">6075</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #0000ff;">$current</span> = get_settings<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'active_plugins'</span><span style="color: #66cc66;">&#41;</span>;
		<a href="http://www.php.net/array_splice" class="liexternal"><span style="color: #000066;">array_splice</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$current</span>, <a href="http://www.php.net/array_search" class="liexternal"><span style="color: #000066;">array_search</span></a><span style="color: #66cc66;">&#40;</span> plugin_basename<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">__FILE__</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #0000ff;">$current</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">&#41;</span>;
		update_option<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'active_plugins'</span>, <span style="color: #0000ff;">$current</span><span style="color: #66cc66;">&#41;</span>;
		do_action<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'deactivate_'</span>.plugin_basename<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">__FILE__</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre>
<p>You can download the complete sourcecode for the plugin <a href="http://wordpress.nazgul.nu/wp-content/uploads/2007/09/importerplugin.phps" title="Importer Plugin example code" class="liinternal">here</a>.</p>
<p>That's it for now. Stay tuned for more posts about new 2.3 features.</p>
<!-- google_ad_section_end -->]]></content:encoded>
			<wfw:commentRss>http://wordpress.nazgul.nu/2007/09/14/new-in-23-importer-plugins/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
