affiliate-toolkit registers taxonomies for your products: the product category plus every custom taxonomy you create in the Custom Fields settings or as a field of type "Taxonomy" in a field group. Two filters let you change how these taxonomies are registered, for example to use the product category on normal posts as well or to expose a taxonomy in the REST API.
Put the code in the functions.php of your child theme or in a small site-specific plugin. Both filters run while the product post type is registered on init.
Change the post types of a taxonomy
The filter atkp_taxonomy_posttypes runs once per taxonomy and decides which post types it is attached to. By default that is only atkp_product. The Custom Fields module adds atkp_fieldgroup to the product category (so field groups can be assigned to it), and the Import Sets module adds atkp_importset.
/**
* Also assign the product category to normal posts.
*
* @param array $post_types Post types the taxonomy is registered for.
* @param atkp_udtaxonomy $taxonomy The taxonomy definition (object, not a string).
*/
function my_atkp_taxonomy_posttypes( $post_types, $taxonomy ) {
if ( $taxonomy->isproductcategory ) {
$post_types[] = 'post';
}
return $post_types;
}
add_filter( 'atkp_taxonomy_posttypes', 'my_atkp_taxonomy_posttypes', 10, 2 );| Parameter | Type | Description |
|---|---|---|
$post_types | array | Post types the taxonomy is registered for. Default: array( 'atkp_product' ), extended by active modules. |
$taxonomy | object (atkp_udtaxonomy) | The taxonomy definition. Useful properties: name, caption, captionplural, isproductcategory, isproductgroup, issystemfield, isnewtax (created in a field group). |
| Return value | array | The modified list of post types. |
The second parameter is an object, not the taxonomy name. Compare
$taxonomy->nameor use a flag such as$taxonomy->isproductcategory. Older examples that compare the parameter with a string never match.
Change the registration arguments
The filter atkp_register_taxonomy runs directly before register_taxonomy() and receives the complete argument array.
/**
* Make the custom taxonomy "brand" available in the block editor and REST API.
*
* @param array $taxargs Arguments for register_taxonomy().
* @param string $taxonomy_name Name of the taxonomy, e.g. "brand".
*/
function my_atkp_register_taxonomy( $taxargs, $taxonomy_name ) {
if ( 'brand' === $taxonomy_name ) {
$taxargs['show_in_rest'] = true;
}
return $taxargs;
}
add_filter( 'atkp_register_taxonomy', 'my_atkp_register_taxonomy', 20, 2 );| Parameter | Type | Description |
|---|---|---|
$taxargs | array | Arguments for register_taxonomy(). |
$taxonomy_name | string | Name (slug) of the taxonomy, for example brand. |
| Return value | array | The modified arguments. |
Defaults set by the plugin:
| Argument | Default |
|---|---|
hierarchical | true |
public | false (the Frontend Product Pages module sets it to true with priority 10) |
show_ui | true |
show_in_menu | false (the terms are managed from the affiliate-toolkit menu) |
show_admin_column | true for the product category, otherwise false |
query_var | true |
capabilities | manage, edit, delete and assign terms all require edit_posts |
Notes
- The name of the product category taxonomy depends on the site language when the plugin was set up:
productcategoryon English sites,produktkategorieon German sites. Checking$taxonomy->isproductcategoryavoids that problem. - A taxonomy whose name already exists (registered by another plugin or the theme) is skipped by affiliate-toolkit, and the filters are not called for it.
- Renaming a taxonomy afterwards is not supported. After changing taxonomies, save the permalinks once (Settings → Permalinks → Save Changes).