How to add support for presently missing registration parameters

While we strive to provide fields for as many post type and taxonomy parameters as we can, we admit we do not have 100% of all available parameters. We do try to provide the most useful fields to our users. On occasion, people run into parameters that just aren’t supported yet. If you are in that position, do not fret. You are not out of luck.

Adding missing parameters to post types before registration.

Right before the registration of post types, we run the parameters through a filter for last-second modifications by others. Because of this, you are able to add support for the missing parameter just in time.

All code examples should be placed in your active theme’s functions.php file or into your own custom plugin.

Code snippet from in Custom Post Type UI for the post type arguments

This is not part that you copy/paste to customize the arguments

/**
 * Filters the arguments used for a post type right before registering.
 *
 * @since 1.0.0
 * @since 1.3.0 Added original passed in values array
 *
 * @param array  $args      Array of arguments to use for registering post type.
 * @param string $value     Post type slug to be registered.
 * @param array  $post_type Original passed in values for post type.
 */
$args = apply_filters( 'cptui_pre_register_post_type', $args, $post_type['name'], $post_type );

return register_post_type( $post_type['name'], $args );

The cptui_pre_register_post_type passes in the $args variable, the current post type slug that is being registered, and the original array of post type data from the CPTUI settings.

The $args variable is going to hold an array of data that matches the proper parameter for the register_post_type function. This array will have all the calculated values from the CPTUI settings ready to use. Refer to the
register_post_type WordPress codex page for more information all of the parameters. With this variable, you should be able to add your own array index values and return the new array.

Unless you’re wanting to have the custom setting applied to all of the post types you’re registering, you are going to want to utilize the second filter parameter to conditionally add the setting.

Example amending the post type arguments

These are older examples. “Delete with user” UI fields were added in 1.6.0.

function my_cptui_amend_post_type_args( $args, $post_type_slug, $post_type_settings ) {
	// Set the post type posts to be deleted when their author's user is deleted.
	$args['delete_with_user'] = true;

	return $args;
}
add_filter( 'cptui_pre_register_post_type', 'my_cptui_amend_post_type_args', 10, 3 );
function my_cptui_amend_post_type_args( $args, $post_type_slug, $post_type_settings ) {
	// Set to delete the posts when the author's user is deleted, only for the `movie` post type.
	if ( 'movie' === $post_type_slug ) {
		$args['delete_with_user'] = true;
	}
	
	return $args;
}
add_filter( 'cptui_pre_register_post_type', 'my_cptui_amend_post_type_args', 10, 3 );

Don’t use both examples at the same time unless you plan to rename one of the functions. Using both, as is, will result in a PHP error due to duplicate function name.

Adding missing parameters to taxonomies before registration.

Just like with post types, we also run the determined taxonomy parameters through a filter for last-second modifications. 

Code snippet from in Custom Post Type UI for the taxonomy arguments

This is not part that you copy/paste to customize the arguments

/**
 * Filters the arguments used for a taxonomy right before registering.
 *
 * @since 1.0.0
 * @since 1.3.0 Added original passed in values array
 *
 * @param array  $args     Array of arguments to use for registering taxonomy.
 * @param string $value    Taxonomy slug to be registered.
 * @param array  $taxonomy Original passed in values for taxonomy.
 */
$args = apply_filters( 'cptui_pre_register_taxonomy', $args, $taxonomy['name'], $taxonomy );

return register_taxonomy( $taxonomy['name'], $object_type, $args );

Just like with the post type filter, the cptui_pre_register_taxonomy filter filters the $args variable used for the taxonomy being registered. It also passes in the current taxonomy slug being registered, and the original array of taxonomy data. Refer to the
register_taxonomy WordPress Codex page for more information all of the parameters. Once again, you can add your own array index values and return the new array.

Example amending the post type arguments

These are older examples. A field for specifying custom callbacks was added in 1.6.0.

function my_cptui_amend_taxonomy_args( $args, $taxonomy_slug, $taxonomy_settings ) {
	// Set the callback for the taxonomy metaboxes to be "my_custom_function". You would need to define that
	// function yourself and have it handle registering the metabox output.
	$args['meta_box_cb'] = 'my_custom_function';

	return $args;
}
add_filter( 'cptui_pre_register_taxonomy', 'my_cptui_amend_taxonomy_args', 10, 3 );
function my_cptui_amend_taxonomy_args( $args, $taxonomy_slug, $taxonomy_settings ) {
	// Set the callback for the taxonomy metaboxes to be "my_custom_function". You would need to define that
	// function yourself and have it handle registering the metabox output.
	if ( 'genre' === $taxonomy_slug ) {
		$args['meta_box_cb'] = 'my_custom_function';
	}
	
	return $args;
}
add_filter( 'cptui_pre_register_taxonomy', 'my_cptui_amend_taxonomy_args', 10, 3 );

Don’t use both examples at the same time unless you plan to rename one of the functions. Using both, as is, will result in a PHP error due to duplicate function name.

Updated Dec 12, 2022 9:09 PM