How to Convert Plugins to Use Custom Post Types

How to Convert Plugins to Use Custom Post Types

If you’re a plugin developer whose been using custom tables for storing information in WordPress the transition to custom post types may be daunting.

However, as WordCamp UK showed there’s no need to as it’s a really simple process, thanks to a demonstration by Kieran O’Shea.

It’s really simple, and here are the two steps;

1. Create your custom post type

Create, install and activate the following plugin:

/*
Plugin Name: Example Custom Post
Plugin URI: http://www.kieranoshea.com
Description: Allows the demonstration of custom posts
Author: Kieran O'Shea
Author URI: http://www.kieranoshea.com
Version: 1.0
*/

function create_post_type() {
register_post_type( 'calendar_event',
array(
'labels' => array(
'name' => __( 'Events' ),
'singular_name' => __( 'Event' ),
'add_new' => __('Add New'),
'add_new_item' => __('Add New Event'),
'edit_item' => __('Edit Event')
),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'events')
)
);
}
add_action( 'init', 'create_post_type' );

function my_rewrite_flush() {
create_post_type();
flush_rewrite_rules();
}
register_activation_hook(__FILE__, 'my_rewrite_flush');

Note the last few lines, especially flush_rewrite_rules() which make sure your new post type can be viewed on your site.

2. Move your existing content to your new custom post type

Now all you need to do is loop through your database entries and add them as new posts for your new custom post type!

/*
A quick command line script to migrate a table of data to custom posts
*/

// Turn off errors as we're sort of outside wordpress
// and the browser and that can cause warnings
define('WP_DEBUG', false);

// Require the standard WordPress header
require(dirname(__FILE__).'/wp-blog-header.php');

// Tell the user what we're about to do
echo 'Migrating table WP_CALENDAR to custom post type CALENDAR_EVENT
';

// We need some of the WordPress globals to proceed
global $wpdb;

// Get all our events
$events = $wpdb->get_results("SELECT * FROM wp_calendar");

// How many are there?
echo sizeof($events).' events will be imported...

';

// Loop through 'em and load 'em to WordPress
if (!empty($events))
{
foreach ($events as $event)
{
$ent['post_type'] = 'calendar_event';
$ent['post_content'] = $event->event_desc;
$ent['post_parent'] = 0;
$ent['post_author'] = 1;
$ent['post_status'] = 'publish';
$ent['post_title'] = $event->event_title;
$entid = wp_insert_post ($ent);
if ($entid == 0) {
echo 'Failed to migrate event "'.$event->event_title.'"
';
} else {
echo 'Migrated event "'.$event->event_title.'"
'; }
}
}

// Tell the user we're all done
echo '
Done!
';

Now, you need to run that from the command line. So, grab yourself a tool like PuTTY for Windows or use the Mac Terminal and connect to your server to run the script.

FREE EBOOK
Your step-by-step roadmap to a profitable web dev business. From landing more clients to scaling like crazy.

By downloading this ebook I consent to occasionally receive emails from WPMU DEV.
We keep your email 100% private and do not spam.

FREE EBOOK
Plan, build, and launch your next WP site without a hitch. Our checklist makes the process easy and repeatable.

By downloading this ebook I consent to occasionally receive emails from WPMU DEV.
We keep your email 100% private and do not spam.

Job done! You’ll see a new menu item in your dashboard with all your new custom posts.

Thankfully, Kieran has provided a mini site for WordCamp UK where you can see his full presentation and download the code above too.

I spoke to Kieran afterwards who provided a useful summary of his session:

Tags: