With three filters you can change which template affiliate-toolkit uses, without editing a single shortcode. That is handy when you want to swap a system template for your own design across the whole site, or pick a template based on conditions such as the list, the shop or the device.
Add the code to the functions.php of your child theme or to a small site-specific plugin.
Available filters
| Filter | Called for | Parameters |
|---|---|---|
atkp_modify_template | Products, lists, product search results (live lists) and search forms | $template |
atkp_modify_template_list | Lists ([atkp_list]), after atkp_modify_template | $template, $id (list ID), $content |
atkp_modify_template_product | Single products ([atkp_product]), after atkp_modify_template | $template, $id (product ID), $content |
| Parameter | Type | Description |
|---|---|---|
$template | string | The template from the shortcode or block: either the name of a system template (for example wide, box, secondwide, bestseller, grid_3_columns) or the ID of a template from affiliate-toolkit → Templates. |
$id | int | ID of the list or product that is being rendered. |
$content | string | The content between the opening and closing shortcode tag (empty if not used). |
| Return value | string | The template that should be used. |
Replace a template everywhere
atkp_modify_template receives only the template. Use it for global rules, for example to redirect a system template to your own template.
/**
* Replace the system template "wide" with your own template (ID 123) everywhere.
*
* @param string $template System template name (e.g. "wide") or template ID.
* @return string
*/
function my_atkp_modify_template( $template ) {
if ( 'wide' === $template ) {
$template = '123';
}
return $template;
}
add_filter( 'atkp_modify_template', 'my_atkp_modify_template' );Choose a template for lists
atkp_modify_template_list also knows the list ID and the shortcode content.
/**
* Show list 456 as a two-column grid on phones.
*
* @param string $template Template name or ID.
* @param int $id ID of the list.
* @param string $content Content of the shortcode.
* @return string
*/
function my_atkp_modify_template_list( $template, $id, $content ) {
if ( 456 === (int) $id && wp_is_mobile() ) {
$template = 'grid_2_columns';
}
return $template;
}
add_filter( 'atkp_modify_template_list', 'my_atkp_modify_template_list', 10, 3 );
wp_is_mobile()checks the user agent on the server. If you use a page cache, make sure it keeps separate versions for mobile devices.
Choose a template for products
atkp_modify_template_product works the same way for single products. It runs after the plugin has switched unavailable products to the configured "not available" template (if that option is enabled), so you can also override that decision.
/**
* Use template 789 for every product of one specific shop.
*
* @param string $template Template name or ID.
* @param int $id ID of the product.
* @param string $content Content of the shortcode.
* @return string
*/
function my_atkp_modify_template_product( $template, $id, $content ) {
$shop_id = get_post_meta( $id, 'atkp_product_shopid', true );
if ( '42' === (string) $shop_id ) {
$template = '789';
}
return $template;
}
add_filter( 'atkp_modify_template_product', 'my_atkp_modify_template_product', 10, 3 );Replace 42 with the ID of your shop and 789 with the ID of your template.