Wordpress - Use register_post_type() to modify an existing post type
After some research I found none of these answers are up to date.
As of December 8, 2015 WordPress includes a new filter, register_post_type_args
, which lets you hook into the arguments of a registered post type.
function wp1482371_custom_post_type_args( $args, $post_type ) {
if ( $post_type == "animal-species" ) {
$args['rewrite'] = array(
'slug' => 'animal'
);
}
return $args;
}
add_filter( 'register_post_type_args', 'wp1482371_custom_post_type_args', 20, 2 );
Is this function really for modifying post types
Yes.
and if so, can you simply redeclare a couple arguments and leave the rest alone?
No. If you want to modify arguments to a post type, you need to use get_post_type_object
to get the post type object, modify what you want in it, then re-register it using your modified type as the new $args parameter.
Here's an example of how to use the 'registered_post_type'
filter to modify a post type in another plugin.
A plugin I was using didn't include a menu_icon in it's definition, so I wanted to add one of my own.
<?php
/**
* Add a menu icon to the WP-VeriteCo Timeline CPT
*
* The timeline plugin doesn't have a menu icon, so we hook into 'registered_post_type'
* and add our own.
*
* @param string $post_type the name of the post type
* @param object $args the post type args
*/
function wpse_65075_modify_timeline_menu_icon( $post_type, $args ) {
// Make sure we're only editing the post type we want
if ( 'timeline' != $post_type )
return;
// Set menu icon
$args->menu_icon = get_stylesheet_directory_uri() . '/img/admin/menu-timeline.png';
// Modify post type object
global $wp_post_types;
$wp_post_types[$post_type] = $args;
}
add_action( 'registered_post_type', 'wpse_65075_modify_timeline_menu_icon', 10, 2 );