Skip to content

Instantly share code, notes, and snippets.

View kjbrum's full-sized avatar
🤘

Kyle Brumm kjbrum

🤘
View GitHub Profile
@kjbrum
kjbrum / search_for_value.php
Last active March 20, 2021 06:24
Check if a value exists in an array/object.
<?php
/**
* Check if an array is a multidimensional array.
*
* @param array $arr The array to check
* @return boolean Whether the the array is a multidimensional array or not
*/
function is_multi_array( $x ) {
if( count( array_filter( $x,'is_array' ) ) > 0 ) return true;
return false;
@kjbrum
kjbrum / remove_array_keys.php
Last active November 11, 2015 20:38
Remove specified keys from an array.
<?php
/**
* Remove specified keys from an array.
*
* @param array &$array The reference to the array to be modified
* @param array $keys An array of keys to be removed
* @return void
*/
function remove_array_keys( &$array, $keys ) {
foreach( $array as $key => &$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) {
@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 / 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 / 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 / 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 / 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 / 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 / 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);