{"id":53534,"date":"2011-07-22T10:00:39","date_gmt":"2011-07-22T14:00:39","guid":{"rendered":"http:\/\/wpmu.org\/?p=53534"},"modified":"2013-04-27T20:49:09","modified_gmt":"2013-04-28T00:49:09","slug":"how-to-convert-plugins-to-use-custom-post-types","status":"publish","type":"post","link":"https:\/\/wpmudev.com\/blog\/how-to-convert-plugins-to-use-custom-post-types\/","title":{"rendered":"How to Convert Plugins to Use Custom Post Types"},"content":{"rendered":"<p>If you&#8217;re a plugin developer whose been using custom tables for storing information in WordPress the transition to custom post types may be daunting.<\/p>\n<p>However, as WordCamp UK showed there&#8217;s no need to as it&#8217;s a really simple process, thanks to a demonstration by <a href=\"https:\/\/wcuk.kieranoshea.com\/archive\/\" target=\"_blank\">Kieran O&#8217;Shea<\/a>.<\/p>\n<p>It&#8217;s really simple, and here are the two steps;<\/p>\n<h3>1. Create your custom post type<\/h3>\n<p>Create, install and activate the following plugin:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n\/*\r\nPlugin Name: Example Custom Post\r\nPlugin URI: http:\/\/www.kieranoshea.com\r\nDescription: Allows the demonstration of custom posts\r\nAuthor: Kieran O&#039;Shea\r\nAuthor URI: http:\/\/www.kieranoshea.com\r\nVersion: 1.0\r\n*\/\r\n\r\nfunction create_post_type() {\r\nregister_post_type( &#039;calendar_event&#039;,\r\narray(\r\n&#039;labels&#039; =&gt; array(\r\n&#039;name&#039; =&gt; __( &#039;Events&#039; ),\r\n&#039;singular_name&#039; =&gt; __( &#039;Event&#039; ),\r\n&#039;add_new&#039; =&gt; __(&#039;Add New&#039;),\r\n&#039;add_new_item&#039; =&gt; __(&#039;Add New Event&#039;),\r\n&#039;edit_item&#039; =&gt; __(&#039;Edit Event&#039;)\r\n),\r\n&#039;public&#039; =&gt; true,\r\n&#039;has_archive&#039; =&gt; true,\r\n&#039;rewrite&#039; =&gt; array(&#039;slug&#039; =&gt; &#039;events&#039;)\r\n)\r\n);\r\n}\r\nadd_action( &#039;init&#039;, &#039;create_post_type&#039; );\r\n\r\nfunction my_rewrite_flush() {\r\ncreate_post_type();\r\nflush_rewrite_rules();\r\n}\r\nregister_activation_hook(__FILE__, &#039;my_rewrite_flush&#039;);\r\n<\/pre>\n<p>Note the last few lines, especially flush_rewrite_rules() which make sure your new post type can be viewed on your site.<\/p>\n<h3>2. Move your existing content to your new custom post type<\/h3>\n<p>Now all you need to do is loop through your database entries and add them as new posts for your new custom post type!<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n\/*\r\nA quick command line script to migrate a table of data to custom posts\r\n*\/\r\n\r\n\/\/ Turn off errors as we&#039;re sort of outside wordpress\r\n\/\/ and the browser and that can cause warnings\r\ndefine(&#039;WP_DEBUG&#039;, false);\r\n\r\n\/\/ Require the standard WordPress header\r\nrequire(dirname(__FILE__).&#039;\/wp-blog-header.php&#039;);\r\n\r\n\/\/ Tell the user what we&#039;re about to do\r\necho &#039;Migrating table WP_CALENDAR to custom post type CALENDAR_EVENT\r\n&#039;;\r\n\r\n\/\/ We need some of the WordPress globals to proceed\r\nglobal $wpdb;\r\n\r\n\/\/ Get all our events\r\n$events = $wpdb-&gt;get_results(&quot;SELECT * FROM wp_calendar&quot;);\r\n\r\n\/\/ How many are there?\r\necho sizeof($events).&#039; events will be imported...\r\n\r\n&#039;;\r\n\r\n\/\/ Loop through &#039;em and load &#039;em to WordPress\r\nif (!empty($events))\r\n{\r\nforeach ($events as $event)\r\n{\r\n$ent&#x5B;&#039;post_type&#039;] = &#039;calendar_event&#039;;\r\n$ent&#x5B;&#039;post_content&#039;] = $event-&gt;event_desc;\r\n$ent&#x5B;&#039;post_parent&#039;] = 0;\r\n$ent&#x5B;&#039;post_author&#039;] = 1;\r\n$ent&#x5B;&#039;post_status&#039;] = &#039;publish&#039;;\r\n$ent&#x5B;&#039;post_title&#039;] = $event-&gt;event_title;\r\n$entid = wp_insert_post ($ent);\r\nif ($entid == 0) {\r\necho &#039;Failed to migrate event &quot;&#039;.$event-&gt;event_title.&#039;&quot;\r\n&#039;;\r\n} else {\r\necho &#039;Migrated event &quot;&#039;.$event-&gt;event_title.&#039;&quot;\r\n&#039;; }\r\n}\r\n}\r\n\r\n\/\/ Tell the user we&#039;re all done\r\necho &#039;\r\nDone!\r\n&#039;;\r\n<\/pre>\n<p>Now, you need to run that from the command line. So, grab yourself a tool like <a href=\"http:\/\/www.chiark.greenend.org.uk\/~sgtatham\/putty\/\" target=\"_blank\">PuTTY for Windows<\/a> or use the Mac Terminal and connect to your server to run the script.<\/p>\n<p>Job done! You&#8217;ll see a new menu item in your dashboard with all your new custom posts.<\/p>\n<p>Thankfully, Kieran has provided a <a href=\"https:\/\/wcuk.kieranoshea.com\/archive\/\" target=\"_blank\">mini site for WordCamp UK<\/a> where you can see his full presentation and download the code above too.<\/p>\n<p>I spoke to Kieran afterwards who provided a useful summary of his session:<br \/>\n<span class=\"embed-youtube\" style=\"text-align:center; display: block;\"><span class=\"embed-youtube-lazy-id dev-hidden\">hDFYULiDWz8<\/span><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you&#8217;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&#8217;s no need to as it&#8217;s a really simple process, thanks to a demonstration by Kieran O&#8217;Shea. It&#8217;s really simple, and here are the two steps; [&hellip;]<\/p>\n","protected":false},"author":6262,"featured_media":207478,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"blog_reading_time":"","wds_primary_category":0,"wds_primary_tutorials_categories":0,"footnotes":""},"categories":[4],"tags":[1041,10175],"tutorials_categories":[],"class_list":["post-53534","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-plugins","tag-custom-post-types","tag-wordcamp"],"_links":{"self":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts\/53534","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/users\/6262"}],"replies":[{"embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/comments?post=53534"}],"version-history":[{"count":5,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts\/53534\/revisions"}],"predecessor-version":[{"id":198841,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts\/53534\/revisions\/198841"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/media\/207478"}],"wp:attachment":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/media?parent=53534"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/categories?post=53534"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/tags?post=53534"},{"taxonomy":"tutorials_categories","embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/tutorials_categories?post=53534"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}