Add your own PHP objects to the Blade engine

Use the atkp_modify_bladedata filter to pass your own PHP objects or values into every affiliate-toolkit Blade template.

affiliate-toolkit renders product boxes, lists and search forms with the Blade template engine. Every template receives a fixed set of variables such as $products and $formatter. With the atkp_modify_bladedata filter you add your own objects or values to that set, so you can call your own helper class directly inside a template.

You need this when a template should show data that the plugin does not provide, for example values calculated by your own code. Prerequisite: you can add PHP code to your site, ideally in a small custom plugin or the functions.php of a child theme.

Why not write PHP in the template?

Templates you create in the template editor are stored in the database. For security reasons affiliate-toolkit strips raw PHP from them before rendering: <?php ?> tags and the Blade directives @php, @endphp and @inject are removed. The filter is the safe way to bring your own logic into a template, because the PHP code stays in your plugin or theme.

The PHP filtering can be switched off under Settings → Advanced settings, but the plugin itself warns that this allows arbitrary code execution. Use the filter instead.

Example

Register a callback for the filter, add your entries to the array and return it. The array key becomes the variable name in the template.

add_filter( 'atkp_modify_bladedata', 'my_atkp_bladedata' );

function my_atkp_bladedata( $bladedata ) {
    // available in every Blade template as $myhelper
    $bladedata['myhelper'] = new My_Template_Helper();

    // simple values work as well: $greeting
    $bladedata['greeting'] = 'Hello';

    return $bladedata;
}

In the template you can then use both variables:

@foreach($products as $product)
    <p>{{ $greeting }}, {{ $product->title }}</p>
    <p>{{ $myhelper->energy_label($product) }}</p>
@endforeach

Use add_filter, not add_action: the plugin calls the hook with apply_filters() and uses the returned array. Always return the array, otherwise the template receives no data at all.

Parameters

NameTypeDescription
$bladedataarrayAll variables passed to the template. The key is the variable name, the value its content.
Return valuearrayThe (extended) array. It is passed to the Blade engine unchanged.

Keys that are already set

Product and list templates receive:

KeyContent
productsArray of the products to render (empty for shop templates).
shopsArray of shops (filled for shop templates such as the price comparison, otherwise empty).
shopThe shop of the first product, or the first shop for shop templates.
formatterThe atkp_formatter object with the placeholder functions (title, price, buttons …).
translatorThe atkp_translator object for the translated texts.
parametersThe template parameters of the current shortcode (template ID, button type, alignment …).

Search form templates of the Frontend Search & Filter module receive controlhelper, formatter, parameters and translator. The same filter runs there, so your additions are available in search forms too.

Do not overwrite the existing keys unless you know what you are doing. Pick a unique key such as myhelper for your own data.