Skip to content

Instantly share code, notes, and snippets.

View kjbrum's full-sized avatar
🤘

Kyle Brumm kjbrum

🤘
View GitHub Profile
@kjbrum
kjbrum / scrollWindow.js
Last active January 12, 2018 21:02
Scroll window to an element
/**
* Scroll window to an element
*/
$('a[href^="#"]').not('a[href="#"]').click(function(e) {
e.preventDefault();
$('html, body').animate({
scrollTop: $($(this).attr('href')).offset().top
}, 500);
});
@kjbrum
kjbrum / wp_first_paragraph_excerpt.php
Last active April 16, 2020 12:08
Create a custom excerpt from the first paragraph of the content.
<?php
/**
* Create a custom excerpt string from the first paragraph of the content.
*
* @param integer $id The id of the post
* @return string $excerpt The excerpt string
*/
function wp_first_paragraph_excerpt( $id=null ) {
// Set $id to the current post by default
if( !$id ) {
@kjbrum
kjbrum / clean.php
Last active November 11, 2015 20:36
Clean a string.
<?php
/**
* Clean a string.
*
* @param string $string The string that needs to be cleaned
* @return string The string that has been cleaned
*/
function clean( $string, $divider='-', $camelcase=false ) {
// Remove special characters
$string = preg_replace('/[^\w]+/', ' ', $string);
@kjbrum
kjbrum / sendMandrillEmail.js
Last active August 29, 2015 14:23
Send an email using Mandrill's API
/**
* Send an email using Mandrill's API
*
* @return {void}
*/
var sendMandrillEmail = function() {
$.ajax({
type: "POST",
url: "https://mandrillapp.com/api/1.0/messages/send.json",
data: {
@kjbrum
kjbrum / wp_gravity_forms_submit_button.php
Last active August 27, 2021 12:55
Change a Gravity Forms submit input to a button.
<?php
/**
* Change a Gravity Forms submit input to a button.
*
* @param string $button The string of the submit input html
* @param object $form A Gravity Form form object
* @return string Our modified submit button string
*/
function wp_gravity_forms_submit_button( $button, $form ) {
return "<button class='btn' id='gform_submit_button_{$form["id"]}'>{$form['button']['text']}</button>";
@kjbrum
kjbrum / wp_password_protect_children.php
Last active May 21, 2016 02:17
Password protect child pages so you don't have to create a password for each page.
<?php
/**
* Create a custom length content string.
*
* @param integer post ID of the parent page
* @return void
*/
function wp_password_protect_children( $post ){
foreach( get_post_ancestors( $post ) as $parent ) {
if( post_password_required( $parent ) ) {
@kjbrum
kjbrum / rgbToHsl.js
Created April 13, 2015 16:05
Convert an RGB color value to HSL
/**
* Convert an RGB color value to HSL
*
* @param Number r The red color value (0-255)
* @param Number g The green color value (0-255)
* @param Number b The blue color value (0-255)
* @return Array The HSL representation
*/
function rgbToHsl(r, g, b){
r /= 255, g /= 255, b /= 255;
@kjbrum
kjbrum / sortArrayByKey.js
Last active August 29, 2015 14:18
Sort an array by a key value
/**
* Sort an array by a key value
*
* @param array array The array that needs to be sorted
* @param string key The value of the key to sort by
* @return array The sorted array
*/
function sortArrayByKey(array, key) {
return array.sort(function(a, b) {
var x = a[key];
@kjbrum
kjbrum / date_difference.php
Last active August 4, 2017 21:42
Calculate the difference between two dates.
<?php
/**
* Calculate the difference between two dates.
*
* @param string $int The interval to calculate (y = years, m = months, d = days, h = hours, i = minutes, s = seconds)
* @param string $d1 The first date (YYYYMMDD)
* @param string $d2 The second date (YYYYMMDD)
* @param mixed $round Whether to return a decimal value, or round up or down
* @param boolean $absolute Whether the value should always be positive
* @return integer $results The calculated value
@kjbrum
kjbrum / calculateMonthlyPayments.js
Last active August 29, 2015 14:17
Calculate the monthly payments for paying off a loan
/**
* Calculate the monthly payments for paying off a loan
* Source: http://www.1728.org/loanform.htm
*
* @param integer apr APR of the loan
* @param integer term Length of repaying the loan in months
* @param integer loanAmount Amount that is being borrowed
* @return integer The monthly payments amount
*/
function calculateMonthlyPayments(apr, term, loanAmount) {