Merge pull request #34 from benchmarkstudios/pdcreis-patch-2

Update functions.php
This commit is contained in:
Pedro Reis 2020-03-05 12:17:11 +00:00 committed by GitHub
commit 8bbf216600
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 84 additions and 0 deletions

View File

@ -77,6 +77,90 @@ function barebones_enqueue_scripts() {
add_action( 'wp_enqueue_scripts', 'barebones_enqueue_scripts' );
/**
* Add async and defer attributes to enqueued scripts
*
* @param string $tag
* @param string $handle
* @param string $src
* @return void
*/
function defer_scripts( $tag, $handle, $src ) {
// The handles of the enqueued scripts we want to defer
$defer_scripts = [
'SCRIPT_ID'
];
// Find scripts in array and defer
if ( in_array( $handle, $defer_scripts ) ) {
return '<script type="text/javascript" src="' . $src . '" defer="defer"></script>' . "\n";
}
return $tag;
}
add_filter( 'script_loader_tag', 'defer_scripts', 10, 3 );
/**
* Add custom scripts to head
*
* @return string
*/
function add_gtag_to_head() {
// Check is staging environment
if ( strpos( get_bloginfo( 'url' ), '.test' ) !== false ) return;
// Google Analytics
$tracking_code = 'UA-*********-1';
?>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=<?php echo $tracking_code; ?>"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '<?php echo $tracking_code; ?>');
</script>
<?php
}
add_action( 'wp_head', 'add_gtag_to_head' );
/**
* Remove unnecessary scripts
*
* @return void
*/
function deregister_scripts() {
wp_deregister_script( 'wp-embed' );
}
add_action( 'wp_footer', 'deregister_scripts' );
/**
* Remove unnecessary styles
*
* @return void
*/
function deregister_styles() {
wp_dequeue_style( 'wp-block-library' );
}
add_action( 'wp_print_styles', 'deregister_styles', 100 );
/**
* Register nav menus
*