Function Friday #17: clean up custom post types created by a plugin or theme

Every Friday, I’m sharing code snippets that I use to customize WordPress. Feedback/suggestions are always welcome! For more information, check out the first post in the series.


If you have a plugin or theme that creates its own custom post types and/or taxonomies, you might want to override some of them. You shouldn’t edit the plugin/theme code directly, because then you can’t apply future updates without losing your changes.

WordPress has a few functions that will let you do it in a non-invasive way. I most frequently use them to slightly modify a post type that’s been added by a plugin, but you can even use them to remove taxonomies or features from built-in post types like Posts or Pages.

The code

// Remove taxonomies and format from Posts
function drollic_modify_post_type() {
    unregister_taxonomy_for_object_type( 'category', 'post' );
    unregister_taxonomy_for_object_type( 'post_tag', 'post' );
    remove_post_type_support( 'post', 'post-formats' );
}
add_action( 'init', 'drollic_modify_post_type' );

This code will remove two taxonomies (Categories and Tags) and Format from Posts. Here’s the Add New Post screen before:

Taxonomies and format before

And the same screen after adding the code:

Taxonomies and format after

These functions don’t just hide the meta boxes for these taxonomies and features, they remove them completely. Much cleaner!

You can use the unregister_taxonomy_for_object_type function to get rid of any taxonomy. All you need to know is its slug (in this case, category for Categories and post_tag for Tags) and the slug of the post type you’re removing it from (in this case, post).

The remove_post_type_support function lets you remove everything from Featured Image to Excerpt to the Title field from any post type – again, all you need to know is the feature’s slug (in this case, post-formats – there’s a full list on the Codex page) and the slug of the post type you’re removing it from (again, post in this case).

If it doesn’t seem to be working, try adding a high priority to the init hook:

add_action( 'init', 'drollic_modify_custom_post_type', 999 );

Where does it go?

This code should go in a functionality plugin. More thoughts on code location are in the first Function Friday post.

Resources

Leave a Reply

Your email address will not be published.