Many plugins register their own Custom Post Types (CPTs) for managing the content they’re adding to your site. However, there are times when you may want to change the default behavior of the post type. How do you change the arguments for creating the post type without manually changing the code in the plugin?
Enter the register_post_type_args filter.
For example, if you would like to change the slug on a post type that is registered in a plugin, you can do the following:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_filter( 'register_post_type_args', 'lc_change_registered_post_type_slug', 10, 2 ); | |
/** | |
* Change the slug for a registered post type. | |
* | |
* @param array $args Array of arguments for registering a post type. | |
* @param string $post_type Post type key. | |
*/ | |
function lc_change_registered_post_type_slug( $args, $post_type ) { | |
// Make sure we're only modifying our desired post type. | |
if ( 'post_type' != $post_type ) | |
return $args; | |
$args['rewrite'] = array( 'slug' => 'new-slug' ); | |
return $args; | |
} |
You can use this filter to change any of the available arguments for the registered post type.