Skip to content

Instantly share code, notes, and snippets.

View bcole808's full-sized avatar

Ben Cole bcole808

View GitHub Profile
@bcole808
bcole808 / animateSingleNumber.js
Created March 11, 2014 15:59
Animate a number inside of an element from one number to another; makes numbers look like they are counting up or down really fast!
/***************************************************
* Takes a jQuery element $elem and animates the inner HTML if it is a number.
* Counts up to 'final_num' from 'start_num' over 'duration' milliseconds.
***************************************************/
animateSingleNumber : function($elem, final_num, start_num, duration) {
var original_num = $elem.html();
// If not set, take inner HTML of elem
if (!final_num) final_num = parseFloat($elem.html().replace(/,/,''));
@bcole808
bcole808 / get_cached_template_part.php
Created March 5, 2014 17:23
Wordpress template part caching system. Allows parts of a Wordpress template to be stored as site transients in order to speed up the rendering of your theme template parts.
<?php
/**
* Retrieves a template part and caches the output to speed up site
* NOTE: Because we are caching display of posts, we need to make sure to delete the transients when posts are updated or published that might affect these template parts.
*
* Uses this function in conjunction with the WP cache: http://codex.wordpress.org/Function_Reference/get_template_part
*
* @param $slug (string) (required) The slug name for the generic template.
* @param $name (string) (optional) The name of the specialized template.
* @return (string) HTML output of the template part.
@bcole808
bcole808 / arrSortObjsByKey.php
Created March 5, 2014 17:20
Sort a multi-domensional PHP array of objects by key value
<?php
/**
* Sort a multi-domensional array of objects by key value
* Usage: usort($array, arrSortObjsByKey('VALUE_TO_SORT_BY'));
* Expects an array of objects.
*
* @param String $key The name of the parameter to sort by
* @param String $order the sort order
* @return A function to compare using usort
*/
@bcole808
bcole808 / numberAbbreviation.php
Created March 5, 2014 17:18
Shorten large numbers into abbreviations (i.e. 1,500 = 1.5k)
<?php
/**
* Shorten large numbers into abbreviations (i.e. 1,500 = 1.5k)
*
* @param int $number Number to shorten
* @return String A number with a symbol
*/
function numberAbbreviation($number) {
$abbrevs = array(12 => "T", 9 => "B", 6 => "M", 3 => "K", 0 => "");
@bcole808
bcole808 / wp_print_tags.php
Created February 28, 2014 21:21
This file prints out Wordpress tags in a comma separated list and shows how many times each tag is used.
<?php $tags = get_tags(array('orderby'=>'count', 'order'=> 'DESC')); ?>
<?php foreach($tags as $tag) {
echo $tag->name . ',' . $tag->slug . ',' .$tag->count;
echo '<br>';
} ?>
@bcole808
bcole808 / Score boost function.php
Last active August 29, 2015 13:56
Increase a float and then slowly reduce the amount of the increase until there is no longer an increase applied.
<?php
/***************************************************
* This function takes in a float and returns a float that has been incrased based on the number of days elapsed since creation
* The purpose is to boost a number that is newer and slowly decrease that boost until it is no longer applied after the active period has ended.
***************************************************/
// Number of days the boost is active - Bounded by 0 and the
// beginning of the sauce degradation phase.
define(ACTIVE_PERIOD, (float) 5);
@bcole808
bcole808 / characterLimitCounter.js
Last active August 29, 2015 13:56
Character limit indicator for text input fields
/***************************************************
* Character Limit Counter displays an indicator below a text field telling the user how many characters they have left.
*
* INSTRUCTIONS:
* Add the attribute maxlength="XX" to your text input.
* Add the class "characterLimitCounter" to your text input.
***************************************************/
var characterLimitCounter = {
initialize : function() {