facetwp_asset_html
Overview
With this hook, you can customize the HTML of FacetWP’s JavaScript and CSS assets (<script> and <link> tags), before they are output in the footer of the page.
This hooks runs both on all of the main plugin’s assets, and on all add-on assets.
Parameters
- $html | string | The HTML of the asset, as output in the page footer
- $url | string | The URL of the asset, as output in the
srcattribute of the asset HTML
Examples
Add a cookiebot attribute
The following example adds the data-cookieconsent="ignore" attribute (more info) to all of FacetWP’s JavaScript assets, to prevent the Cookiebot plugin from blocking FacetWP’s scripts:
How to use custom PHP code?
PHP code can be added to your (child) theme's functions.php file. Alternatively, you can use the Custom Hooks add-on, or a code snippets plugin. More info
add_filter('facetwp_asset_html', function( $html, $url ) { // Check if it's a <script> tag if ( strpos($html, '<script') !== false ) { // Add the data-cookieconsent attribute $html = str_replace('<script ', '<script data-cookieconsent="ignore" ', $html); } return $html; }, 10, 2);
Because of the check in line 4 code, the the new attribute is only added to <script> tags, and not to <link> tags (CSS files).
With this hook added to the (child) theme’s functions.php, all of FacetWP’s <script> tags will now look like this:
<script data-cookieconsent="ignore" src="https://domain.com/wp-content/plugins/facetwp/assets/js/dist/front.min.js?ver=x.x.x"></script>
Add a “nonce” attribute to FacetWP’s linked scripts
If you are implementing a Strict Content Security Policy (CSP), you’ll notice that you cannot use the wp_script_attributes hook to add the required nonce attribute to FacetWP’s linked scripts. This is because FacetWP intentionally does not use WP enqueue or dequeue functions that this hook runs on. FacetWP’s assets are only loaded when facets are detected on the page, which cannot be done using wp_enqueue_script().
To add the nonce attribute to FacetWP’s linked scripts you can instead use the facetwp_asset_html hook.
Add the following code to your (child) theme’s functions.php to add the nonce attribute if you are using the Strict CSP plugin:
How to use custom PHP code?
PHP code can be added to your (child) theme's functions.php file. Alternatively, you can use the Custom Hooks add-on, or a code snippets plugin. More info
add_filter('facetwp_asset_html', function( $html, $url ) { // Only modify <script src="..."> tags, not <link> CSS tags if ( strpos( $html, '<script') !== false ) { $nonce = \StrictCSP\get_nonce(); // Adapt as needed. This only works with https://wordpress.org/plugins/strict-csp/ $html = str_replace('<script ', '<script nonce="' . esc_attr($nonce) . '" ', $html); } return $html; }, 10, 2);
The exact function to get the nonce value, in line 5, depends on the chosen implementation. See the Strict CSP tutorial for several alternative ways to do this.
Note that for FacetWP’s inline scripts you can use the wp_inline_script_attributes hook, as these scripts are added with the the wp_print_inline_script_tag() function, since version 4.5.