Skip to content

Instantly share code, notes, and snippets.

View kjbrum's full-sized avatar
🤘

Kyle Brumm kjbrum

🤘
View GitHub Profile
@kjbrum
kjbrum / responsive_media.scss
Created June 16, 2016 17:20
Make embedded media elements responsive.
.video-wrapper {
position: relative;
padding-bottom: 56.25%;
height: 0;
iframe,
object,
embed {
position: absolute;
top: 0;
@kjbrum
kjbrum / inViewport.js
Last active March 15, 2017 20:10
Test if an element is in the viewport.
var inViewport = function(el, side) {
var win = window;
var viewport = {
top: win.scrollY,
left: win.scrollX
};
viewport.right = viewport.left + win.outerWidth;
viewport.bottom = viewport.top + win.outerHeight;
@kjbrum
kjbrum / wp-social-share-links.php
Last active November 13, 2017 00:27
Social share links
@kjbrum
kjbrum / str_replace_last.php
Created January 21, 2016 22:43
Replace the last occurrence of a string.
<?php
/**
* Replace the last occurrence of a string.
*
* @param string $search The string to search for
* @param string $replace The string to replace the search string
* @param string $subject The string to search and replace
* @return string The new string
*/
function str_replace_last( $search=null, $replace=null, $subject=null ) {
@kjbrum
kjbrum / array_to_csv.php
Created January 21, 2016 22:42
Convert an array to a CSV string.
<?php
/**
* Convert an array to a CSV string.
*
* @param array $array The array to be converted
* @return string The newly created string
*/
function array_to_csv( $array ) {
if(count( $array ) == 0 ) {
return null;
@kjbrum
kjbrum / setup_script.sh
Last active January 12, 2016 00:13
Dev environment setup script.
#!/usr/bin/env bash
# Ask for the administrator password upfront.
sudo -v
# Keep-alive: update existing `sudo` time stamp until the script has finished.
while true; do sudo -n true; sleep 60; kill -0 "$$" || exit; done 2>/dev/null &
@kjbrum
kjbrum / dir-to-filename.sh
Last active January 11, 2016 23:40
Rename all the files in a directory with their parent directory name.
#!/usr/bin/env bash
# Dir to Filename
# Rename all the files in a directory with their parent directory name.
# Copyright (C) Kyle Brumm <http://kylebrumm.com>
if [ "$1" = "-h" -o "$1" = "--help" ]; then cat <<EOF
Dir to Filename
@kjbrum
kjbrum / current_page_url_in.php
Last active December 10, 2015 01:08
Match the current URL against the supplied URL.
<?php
/**
* Match the current URL against the supplied URL.
*
* @param string $url The URL to be checked
* @return boolean
*/
function current_page_url_in( $url ) {
return ( strpos( $_SERVER['REQUEST_URI'], parse_url( $url, PHP_URL_PATH ) ) !== false );
}
@kjbrum
kjbrum / wp_get_first_category.php
Last active February 8, 2017 03:48
Get the first category of a post.
<?php
/**
* Get the first category of a post.
*
* @param int $id The id of a post
* @param int $id The field value to return
* @return mixed The category object or single field
*/
function wp_get_first_category( $id=null, $field='all' ) {
// Check if only a field value was supplied
@kjbrum
kjbrum / bash_functions.sh
Last active January 10, 2016 06:46
A collection of useful bash functions.
# Variables used
RED=$(tput setaf 1);
GREEN=$(tput setaf 2);
YELLOW=$(tput setaf 3);
WHITE=$(tput setaf 7);
# Throw an error
error() {
printf "${RED}Error:${WHITE} ${1}\n"
exit