Skip to content

Instantly share code, notes, and snippets.

View mrbobbybryant's full-sized avatar

Bobby Bryant mrbobbybryant

View GitHub Profile
@mrbobbybryant
mrbobbybryant / gist:8a59d9bbe6f754155d4d
Created May 27, 2015 14:34
Array_intersect, array_diff, and array_merge examples
<?php
$toppings1 = array("Pepperoni", "Cheese", "Anchovies", "Tomatoes");
$toppings2 = array("Ham", "Cheese", "Peppers");
$inttoppings = array_intersect($toppings1, $toppings2);
$difftoppings = array_diff($toppings1, $toppings2);
$bothtoppings = array_merge($toppings1, $toppings2);
echo '<p> array_intersect returns an array consisting of varables that both original array had in common.</p>';
var_dump($inttoppings);
@mrbobbybryant
mrbobbybryant / gist:2a68c60ce8dd5d98eee4
Last active August 29, 2015 14:21
array_filter example
<?php
//Allows you to difine your own callback function to uniquely filter an array.
function endswithy($value) {
return (substr($value, -1) == 'y');
}
//This example returns an array of people who's names end in "y"
$people = array("Johnny", "Timmy", "Bobby", "Sam", "Tammy", "Danny", "Joe");
$withy = array_filter($people, "endswithy");
var_dump($withy);
?>
@mrbobbybryant
mrbobbybryant / limit_text.php
Created July 30, 2015 22:38
Limit text length
<?php
function limit_ticker_length( $title, $length ) {
if ( strlen( $title ) > $length ) {
$title = substr( $title, 0, ( $length -3 ) );
$title = substr( $title, 0, strrpos( $title,' ' ) ) . '&#8230;';
}
@mrbobbybryant
mrbobbybryant / parse_shortcode_atts.php
Created September 8, 2015 18:07
Extract Shortcode Attributes for a given shortcode on save_post.
<?php
/**
* Extracts Shortcode Attributes from the post content
* @param $content
*
* @uses get_shortcode_regex
* @uses shortcode_parse_atts
*
* @return array
*/
@mrbobbybryant
mrbobbybryant / wp_editor.php
Created November 13, 2015 02:19
example of wp_editor
<?php
$content = get_post_meta( $post->ID, 'principle_duties', true );
if ( ! isset( $content ) ) {
$content = 'What I want it to say';
}
$editor = 'principle_duties';
$settings = array(
'textarea_rows' => 8,
arrayDiff: function( current, selected ) {
return selected.filter( function(data) {
return current.indexOf(data) === -1;
} );
},
extractArrayValue: function( array, value ) {
return array.map( function(data) {
return data[value];
@mrbobbybryant
mrbobbybryant / set_taxonomy_term_term_order.php
Last active November 18, 2015 19:18
Allows you to update the taxonomy term order within the term_relationship table in WordPress
<?php
/**
* Update an objects term order in the term relationships table
* @param int $term_order
* @param int $object_id post_od
* @param int $tt_id term_taxonomy_id
*
* @return int
*/
function set_taxonomy_term_term_order( $term_order, $object_id, $tt_id ) {
@mrbobbybryant
mrbobbybryant / maybe.js
Created December 14, 2015 01:35
This function allows you to only call a function if it has everything it needs to run. So it "May" run.
function maybe(fn) {
return function() {
var i;
if ( arguments.length === 0 ) {
return;
} else {
for(i = 0; i < arguments.length; i++) {
if (arguments[i] == null) return;
}
return fn.apply(this, arguments);
@mrbobbybryant
mrbobbybryant / once.js
Created December 14, 2015 14:38
Ensures a function is only called once.
//Once
function once(fn) {
var done = false;
return function() {
return done ? void 0 : ((done = true), fn.apply(this, arguments));
};
}
var askedOnBlindDate = once(function () {
@mrbobbybryant
mrbobbybryant / events.js
Last active December 17, 2015 20:28
pub/sub pattern
var events = {
events: {},
subscribe: function (eventName, fn) {
this.events[eventName] = this.events[eventName] || [];
this.events[eventName].push(fn);
},
unsubscribe: function(eventName, fn) {
if (this.events[eventName]) {
this.events[ eventName ] = this.events[ eventName ].filter( function( eventFn ) {
return eventFn !== fn;