Adding Custom Code Snippets in WordPress
First, you need to install and activate the free WPCode plugin on your website.
After activation, the plugin will add a new menu item labeled ‘Code Snippets’ to your WordPress admin bar. Click this and you will see a list of all the custom code snippets you have saved on your site.
Since you just installed the plugin, your list will be empty.
Click the ‘Add New’ button to add your first custom snippet to WordPress.
This will bring you to the ‘Add Snippet’ page. Here you can choose a snippet from the pre-made library or add your own custom code.
To add custom code, click on the ‘Use Snippet’ button under the ‘Add Your Custom Code (New Snippet)’ option.
You need to start by entering a title for your custom code snippet. This can be anything that will help you identify the code.
Then you can copy and paste your snippet into the Code Preview box. Make sure you select the right code type from the drop-down menu on the right.
In the screenshot above, we have added a custom code snippet to remove WordPress Google Fonts from our test site.
/** Remove loading gfonts */
add_filter( 'style_loader_src', function( $href ) {
if( strpos( $href , "//fonts.googleapis.com/" ) === false ) {
return $href;
}
return false;
});
// Remove dns-prefetch for fonts.googleapis
add_filter( 'wp_resource_hints', function( $urls ) {
foreach ($urls as $key => $url) {
if ( 'fonts.googleapis.com' === $url ) {
unset( $urls[ $key ] );
}
}
return $urls;
} );
When you have finished setting the options, toggle the switch in the top right corner from ‘Inactive’ to ‘Enabled’ and then click on the ‘Save Particle’ button.
If you want to save the snippet and not activate it, just click on the ‘Save Snippet’ button.
Once you save and activate the snippet, it will be automatically added to your site or displayed as a shortcode, depending on the embedding method you choose.