apply_filters( 'RCB/Templates/Recommended', $recommended, $template ) → {boolean}
This filter is only intended for developers, which addresses the following scenario:
- You are working on a plugin, which embeds e.g. Google Fonts through a custom script which cannot be blocked by our content blocker.
- You now want to build an integration to Real Cookie Banner, your users should be able to get consent for Google Fonts as comfortable as possible.
Note: Google Fonts is only used as an example here to better illustrate the integration. This works with all integrations which need to change the way how Real Cookie Banner recommends services in the Service Scanner.
Filter usage
If you are a plugin developer and you embed e.g. Google Fonts via a custom JavaScript, the service will probably not be found via our scanner because we do not parse custom scripts of plugins and check if they dynamically embed content in any way. Learn more about it here: What does the service scanner find (and what not)?.
You could use the following code snippet to recommend Google Fonts to your user base:
add_filter('RCB/Templates/Recommended', function($recommended, $template) {
if (!$recommended &&
// Recommend the service template instead of content blocker template
property_exists($template, 'technicalDefinitions') &&
$template->identifier === "google-fonts" &&
// google_fonts_embed_active() is a example function which checks if Google Fonts embed is active in your plugin
google_fonts_embed_active()) {
return true;
}
return $recommended;
}, 10, 2);
Where do I find this google-fonts
identifier? This identifier is unique per template and should definitely be used for matching.
You will find the respective identifier when you create the service in Real Cookie Banner, in the form in the field "Unique Identifier".
The filter is not fired, why?
Since the calculation of the templates can be compute-intensive, this is cached of course. Basically, the template cache is emptied every day, and when you activate or deactivate a plugin. If you provide an option in your plugin that disables Google Fonts inclusion, for example, this will not immediately clear the template cache and perform the calculation again.
Should you technically save the option via WordPress' official Options API, you can use the following snippet to
automatically clear the template cache with our wp_rcb_invalidate_templates_cache(true)
API together with
the update_option_{$option}
hook:
<?php
// Replace my_plugin_is_google_fonts_active with your option name
add_action('update_option_my_plugin_is_google_fonts_active', function() {
wp_rcb_invalidate_templates_cache();
});
// Listen also to add_option as it is called for the first time when option is not
// available in update_option
add_action('add_option_my_plugin_is_google_fonts_active', function() {
wp_rcb_invalidate_templates_cache();
});
But this does actually not block Google Fonts?
Since you used a custom script to load Google Fonts, we cannot block this with our blocker mechanism until consent is given. But don't worry, we also provide corresponding APIs to "wait" for a consent in your JavaScript code and load Google Fonts accordingly afterward:
Both consent()
and consentSync()
allows to wait for a consent for a given service. It allows multiple mechanism to identify the service (e.g.
technical definition, HTTP cookie names, ...) but we highly recommend to use the unique identifier:
(window.consentApi?.consent("google-fonts") || Promise.resolve()).then(() => {
console.log("Consent for Google Fonts given, load them...!");
});
Parameters:
Name | Type | Description |
---|---|---|
$recommended |
boolean | |
$template |
ServiceTemplate | BlockerTemplate | See templates |
- Since:
- 3.16.0
Returns:
- Type
- boolean