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
src
attribute of the asset HTML
Usage example
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>