Skip to content

Instantly share code, notes, and snippets.

@TMMC
Last active February 1, 2023 15:52
Show Gist options
  • Save TMMC/44b0a54037b851aa7e163388461788b9 to your computer and use it in GitHub Desktop.
Save TMMC/44b0a54037b851aa7e163388461788b9 to your computer and use it in GitHub Desktop.
Wordpress code snippets
<?php
/* Add excerpt for page posty type */
add_post_type_support('page', 'excerpt');
?>
<?php
/* Add shortcode for current year */
add_shortcode('current_year', 'tmmc_current_year_shortcode');
function tmmc_current_year_shortcode() {
return date('Y');
}
?>
<?php
/* Enable shortcodes in text widgets */
add_filter('widget_text', 'do_shortcode');
?>
<?php
/* Add Google Analytics */
add_action('wp_head', 'tmmc_add_googleanalytics');
function tmmc_add_googleanalytics() { ?>
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-XXXXXXXXXX"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-XXXXXXXXXX');
</script>
<?php }
?>
<?php
/* Add script for animated header */
add_action('wp_footer', 'tmmc_add_animatedheader');
function tmmc_add_animatedheader() { ?>
<script>
jQuery(document).ready(function($) {
// == Basic variables
var win = $(window),
doc = $(document),
root = $('html'),
body = $('body'),
navbarHeight = 70, // Navbar height on desktop
navbarInitHeight = 80, // Initial height
navbarBreakPoint = 992, // Bootstrap var grid-float-breakpoint = screen-md = 992px (or grid-float-breakpoint-max = screen-md - 1)
locationHref = $(location).attr('href'),
locationPathname = $(location).attr('pathname'),
locationHost = $(location).attr('host'),
locationHostname = $(location).attr('hostname'),
locationPort = $(location).attr('port'),
locationQuery = $(location).attr('search');
// == Animated header
win.scroll(function () {
if (doc.scrollTop() >= navbarInitHeight) {
// Class may change to any 'navbar-<target>' or any 'navbar-<dest>'.
$("#masthead").addClass("navbar-animated"); // or removeClass
} else {
$("#masthead").removeClass("navbar-animated"); // or addClass
}
});
});
</script>
<?php }
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment