Skip to content

Instantly share code, notes, and snippets.

View morgyface's full-sized avatar

Dan morgyface

View GitHub Profile
@morgyface
morgyface / change.js
Created April 13, 2025 20:21
JS | Change class when scrolling reaches a div
$(function() {
var header = $(".header");
$(window).scroll(function() {
var scroll = $(window).scrollTop();
var position = header.offset().top;
if (scroll >= position) {
header.removeClass('sticky').addClass("static");
} else {
@morgyface
morgyface / slider.css
Created April 7, 2025 08:27
Simple carousel/slider
.pagination {
display: flex;
}
.numbers {
padding: 0 1em;
}
.slides {
list-style: none;
@morgyface
morgyface / featured.njk
Created October 18, 2023 16:47
11ty | Nunjucks | Featured items using next and previous
@morgyface
morgyface / child_terms.php
Created March 18, 2023 11:45
WordPress | Get Child Terms
<?php
function get_child_terms ( $term, $tax ) {
$child_terms = array();
if( $term && $tax ) {
$the_term = get_term_by( 'slug', $term, $tax ); // Get term object using the slug
$terms = get_the_terms(get_the_ID(), $tax); // Get the terms attached to the post
if( $the_term && $terms ) {
foreach ( $terms as $term ) {
if( $the_term->term_id == $term->parent ) {
$child_terms[] = $term->name; // Only get the terms where the parent is the current term
@morgyface
morgyface / referer_term.php
Last active March 9, 2023 17:03
WordPress | Extract a taxonomy term from the referring URL
<?php
function referer_term( $taxonomy ) {
$term = null;
$referer = wp_get_referer(); // Get the referer URL
if( $referer ) {
// Here we parse the URL extracting the path element of it
$referer_path = parse_url( $referer, PHP_URL_PATH );
if( $referer_path && $taxonomy ) {
// If we have a path and a taxonomy we are good to go
if ( strpos( $referer_path, $taxonomy ) !== false ) {
@morgyface
morgyface / geo_address.php
Last active November 15, 2022 15:34
A PHP function that uses Google Maps Geocode API to return coordinates from a postal address
<?php
function geolocation( $address ) {
$coordinates = null;
if( $address ) {
$address = rawurlencode( $address ); // Returns a string in which all non-alphanumeric characters except -_.~ have been replaced with a percent (%) sign followed by two hex digits.
$key = 'YOUR_API_KEY'; // The Geolocation API Key
$url = 'https://maps.googleapis.com/maps/api/geocode/json?address=' . $address . '&key=' . $key;
$data = file_get_contents( $url ); // Get the JSON file.
if ( $data ) {
$json = json_decode( $data, true ); // Turns the JSON into an array
@morgyface
morgyface / programmatically_added_posts.php
Created June 1, 2022 21:44
Wordpress | Programmatically add posts
<?php
add_action( 'init', 'import_products', 20 );
function import_products() {
$products = array(
array( 'title' => 'AFDD060630B', 'description' => 'AFDD RCBO 10A 30mA 1P+N TYPE A', 'term' => 'Switch'),
array( 'title' => 'AFDD061030B', 'description' => 'AFDD RCBO 12A 30mA 1P+N TYPE A', 'term' => 'Large Dial'),
array( 'title' => 'AFDD061630B', 'description' => 'AFDD RCBO 16A 30mA 1P+N TYPE A', 'term' => 'Rotor')
);
$user = get_user_by('login', 'your-username');
$user_id = $user->ID;
@morgyface
morgyface / first_term.php
Created May 26, 2022 10:28
WordpPress | Get the first term of a taxonomy assigned to a post
<?php
$taxonomy = 'taxonomy';
if( has_term( '', $taxonomy ) ) {
$terms = get_the_terms( $post->ID, $taxonomy );
$term = array_shift( $terms );
$term_link = get_term_link( $term->term_id );
$term_name = $term->name;
}
?>
@morgyface
morgyface / get_handle.php
Created April 21, 2021 15:09
PHP | Get handle from URL
<?php
/**
* Takes a complete URL and returns the user handle
* created for Instagram but also likely to work for other social channels
**/
function get_handle( $url ) {
$url = rtrim( $url, '/' );
$parse = parse_url( $url );
$path = $parse['path'];
$path = ltrim( $path, '/' );
@morgyface
morgyface / sanitize.njk
Created February 3, 2021 15:07
Nunjucks | sanitize | replace whitespace with hyphens
{% set image = profile.name | lower | replace(" ", "-") %}