Top

How to Convert Plugins to Use Custom Post Types

July 22, 2011

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< span class="phpString">' => 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.

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:

Because this plugin/widget is provided free of charge, I simply ask you to link back to Pressography.com in a post on your blog, or from one of your sites. It doesn't need to be anything fancy - just a note saying that you're using it, and whether or not you like it. However, there is no requirement that you do this, you're free to use the plugin regardless.

Comments

Got something to say?

Bottom
Article Archive  |  This site is a historical archive of pressography.com, a WordPress plugin and theme blog that published between 2006 and 2012. It is preserved for reference and is not operated by, affiliated with, or endorsed by its original authors. Nothing here is for sale and no form on this site is active. If you are the original owner and would like this domain returned, please get in touch.