In our previous article, we talked about how and why you should use Integrity and Crossorigin in your script link tag attributes.
For those who are somewhat familiar with web development, you will know that more often or not websites are built using Frameworks or CMS tools, therefore, you may not interact directly with the raw HTML code in order to use the attributes as proposed in our other above-mentioned article.
Because of that, in this guide, we will show you how to to use Integrity and Crossorigin in your WordPress themes.
To get started you will need to add the script in your theme’s functions PHP script under the section where you have enqueued scripts and CSS files. You will first need to register the said script, enqueue the script then add the data. We have a simple example as a guide that will add Bootstrap to your theme.
wp_register_script('Bootstrap4', 'https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/js/bootstrap.min.js', array(), '3.3.1', true); wp_enqueue_script('Bootstrap4'); wp_script_add_data( 'Bootstrap4', array( 'integrity', 'crossorigin' ) , array( 'sha256-CjSoeELFOcH0/uxWu6mC/Vlrc1AARqbm/jiiImDGV3s=', 'anonymous' ) );
There you have it.