Persistent WordPress Custom Post Types
I recently changed the way I create custom post types in WordPress. Usually, you create all the declarations in the
functions.php file, but if you change your theme, you lose the custom post type (the content is still there, just not the custom post type). Here’s how to make your custom post types persistent.
The way I have it now, is I have everything in a custom plugin. Nothing fancy, just a super-basic plugin. The code is exactly the same as in
functions.php, but now it’s in a plugin, so if I change my theme, it keeps all the custom post types.
Here’s how to do just that.
Set up the Plugin
Make a PHP file and name it something unique, like “
mysitename-cpt.php“. At the very top of the file, put some basic meta information:
/* Plugin Name: MYSITE's Custom Post Types Description: The custom post type declarations for my site. Version: 1.0 Author: YOUR NAME Author URI: YOUR WEBSITE License: GPL2 */
Underneath that, simply copy & paste the custom post type and custom post taxonomies from your
functions.php file into this plugin. Make sure you transfer the
add_action() calls for your custom post types/taxonomies as well.
Then remove all the custom post type stuff from your
functions.php file, and make sure you upload and activate your new custom plugin. You’re done!