Skip to content

Instantly share code, notes, and snippets.

Avatar
🔨
Hammering Things Into Place

Tanner Record tarecord

🔨
Hammering Things Into Place
View GitHub Profile
@tarecord
tarecord / get_top_ancestor.php
Created Jun 6, 2017
Get the top most parent of a WordPress post
View get_top_ancestor.php
/**
* Gets the top most ancestor for a post.
*
* @param object $post The current post
*
* @return integer The post ID of the ancestor
*/
function get_top_ancestor( $post ) {
if ( $post->post_parent ) {
$ancestors = get_post_ancestors( $post->ID );
@tarecord
tarecord / redirect_attachment_page_to_file.php
Last active Jun 16, 2017
Redirect any WordPress attachment page to the actual file
View redirect_attachment_page_to_file.php
<?php
/**
* Redirect all attachment pages to actual file
*/
function redirect_attachment_pages() {
global $post;
// Return if not an attachment
if ( !is_attachment() ) {
@tarecord
tarecord / menu_transients.php
Created Jul 5, 2017
A function to display a nav menu saved as a transient
View menu_transients.php
<?php
/**
* Menu Transient, a function that helps with extremely large menus by storing the query in a transient
* for ~24hrs. It can be used in template files to both store the transient and display the menu so there
* is no need for multiple functions and queries.
*
* @param $name string A descriptive name of the menu (ex. "mobile-menu" or "quick-links").
* @param $args array The array of nav menu arguments.
*
* @return string The HTML formatted nav menu
@tarecord
tarecord / docker-compose.yml
Created Nov 27, 2018
A docker-compose configuration for spinning up local WordPress sites
View docker-compose.yml
version: '3.3'
services:
db:
image: mysql:5.7
volumes:
- ./mysql:/var/lib/mysql
ports:
- "3306:3306"
restart: always
@tarecord
tarecord / get_vimeo_id.php
Last active Feb 18, 2020
A function that takes a vimeo url and returns the video ID
View get_vimeo_id.php
<?php
function get_vimeo_id( $url ) {
/**
* Matches:
* https://player.vimeo.com/video/123456789
* https://vimeo.com/123456789
* https://www.vimeo.com/123456789
*/
preg_match( '/(vimeo\.com\/)(?:video\/)?([\d]+)/', $url, $matches );
$vimeo_id = $matches[2];
@tarecord
tarecord / find-font-families.js
Created Jul 14, 2020
Find a list of font families within CSS, SCSS or SASS
View find-font-families.js
// var content = document.querySelector('.my-css-file');
content.match(/font-family:\s[\'|\"](.*)[\'|\"]/g).map(function(string){
var font = string.split(": ");
return font[1].slice(1, -1);
}).filter((v, i, a) => a.indexOf(v) === i)
// returns reduced array of unique font families: ['Times New Roman', 'Source Sans Pro', ... ]
View custom-list-bullets.scss
ul li {
// Remove the old list style.
list-style: none;
position: relative;
}
ul li:before {
content: '';
// Position the pseudo element relative to each list item.
position: absolute;
@tarecord
tarecord / redirect_after_register.php
Created Sep 23, 2020
Sign in With Google - Adjust redirect after successfully authenticating
View redirect_after_register.php
<?php
/**
* functions.php
**/
add_filter( 'siwg_auth_redirect', 'redirect_users_after_login', 10, 1 );
/**
* Filter the url redirection after user authenticates with Google.
**/
@tarecord
tarecord / .git_commit_template
Created Jan 14, 2021
A template to help you write better commits
View .git_commit_template
######## Subject Line (50 characters) ############
# - Use the imperative mood (imagine the words "This will")
# - Capitalize the first letter
# - Limit to 50 characters
# - Do not end the subject line with a period
######## The Body (72 characters per line) #############################
# - Add line breaks so each line is a maximum of 72 characters
@tarecord
tarecord / get_primary_taxonomy_term.php
Last active May 6, 2021
Returns the primary term for the chosen taxonomy set by Yoast SEO or the first term selected.
View get_primary_taxonomy_term.php
<?php
/**
* Returns the primary term for the chosen taxonomy set by Yoast SEO
* or the first term selected.
*
* @link https://www.tannerrecord.com/how-to-get-yoasts-primary-category/
* @param integer $post The post id.
* @param string $taxonomy The taxonomy to query. Defaults to category.
* @return array The term with keys of 'title', 'slug', and 'url'.
*/