Customizing CPT-onomy Settings And Set Default Values

These filters are very helpful for customizing your CPT-onomy settings and allow you to exclude certain data from being selected and/or set default property values.

Some need-to-knows about filters:

  • To use these filters, tweak the code below and add them to your functions.php file.
  • If a filter accepts more than one argument, you have to declare the number of arguments in your add_filter() declaration, along with the filter’s priority.
  • All filters MUST return a value, i.e. the value being filtered.
  • For more information, check out add_filter() in the WordPress codex.

If you don’t want a CPT-onomy to be able to “attach” itself to a particular post type:

<?php
add_filter( 'custom_post_type_onomies_attach_to_post_type_property_include_post_type', 'my_website_attach_to_post_type_property_include_post_type', 1, 3 );
function my_website_attach_to_post_type_property_include_post_type( $include_post_type, $post_type, $post_type_being_edited ) {
   if ( $post_type_being_edited == 'actors' && in_array( $post_type, array( 'actors', 'directors' ) ) )
      return false;
   return $include_post_type;
}
?>

If you don’t want a particular user role to be selected for a CPT-onomy’s “Restrict User’s Capability to Assign Term Relationships”:

(keep in mind that if no user roles are selected, then ALL user roles have permission)

<?php
add_filter( 'custom_post_type_onomies_restrict_user_capabilities_property_include_user_role', 'my_website_restrict_user_capabilities_property_include_user_role', 1, 3 );
function my_website_restrict_user_capabilities_property_include_user_role( $include_user_role, $user_role, $post_type_being_edited ) {
   if ( $user_role == 'editor' )
      return false;
   return $include_user_role;
}
?>

If you don’t want a particular “support” feature, i.e. “title”, “editor”, etc., to be selected for a custom post type:

<?php
add_filter( 'custom_post_type_onomies_supports_property_include_support', 'my_website_supports_property_include_support', 1, 3 );
function my_website_supports_property_include_support( $include_support, $support, $post_type_being_edited ) {
   if ( in_array( $support, array( 'comments', 'thumbnail' ) ) )
      return false;
   return $include_support;
}
?>

If you don’t want a particular taxonomy to be selected for a custom post type:

<?php
add_filter( 'custom_post_type_onomies_taxonomies_property_include_taxonomy', 'my_website_taxonomies_property_include_taxonomy', 1, 3 );
function my_website_taxonomies_property_include_taxonomy( $include_taxonomy, $taxonomy, $post_type_being_edited ) {
   if ( $taxonomy == 'post_tag' )
      return false;
   return $include_taxonomy;
}
?>

If you want to set a default value for a setting:

<?php
add_filter( 'custom_post_type_onomies_default_property_value', 'my_website_default_property_value', 1, 3 );
function my_website_default_property_value( $default_value, $property_key, $property_parent_key ) {
   switch ( $property_key ) {
      case 'description':
         return 'What an awesome CPT-onomy.';
      case 'attach_to_post_type':
         return array( 'actors', 'movies' );
      case 'restrict_user_capabilities':
         return 'author';
      case 'hierarchical':
         return true;
      case 'has_cpt_onomy_archive':
      case 'enable_rewrite':
      case 'feeds':
      case 'can_export':
         return false;
   }
   return $default_value;
}
?>