Skip to content

Instantly share code, notes, and snippets.

@matesnippets
matesnippets / author.js
Created December 11, 2014 10:46
JavaScript - unwrap element
/**
* Loop
* http://stackoverflow.com/questions/157260/whats-the-best-way-to-loop-through-a-set-of-elements-in-javascript
*/
var unWrap = function(e) {
for (var i = 0; e[i]; i++) {
var menuSection = e[i],
menuSectionAnchor = menuSection.children[0],
// Grab the contents of the element
menuSectionAnchorText = menuSectionAnchor.innerHTML,
@matesnippets
matesnippets / get_first_inserted_image
Created December 25, 2014 18:00
PHP, WP, Get the first image inserted into a post body
/**
* Get the first image inserted into a post body
*/
function get_first_inserted_image()
{
global $post, $posts;
preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$first_img = $matches [1] [0];
// Spew it out as an imege
$first_img = '<img alt="Blog Post Image" src="' . $first_img . '">';
@matesnippets
matesnippets / add_image_placeholders
Created December 26, 2014 23:42
PHP, WP, Lazyload images inserted into post body
/**
* Lazy load images inserted into the post body
* @link http://wptheming.com/2013/03/lazy-loading-images/
*/
function add_image_placeholders($content)
{
// Don't lazyload for feeds
if (is_feed()) {
return $content;
}
@matesnippets
matesnippets / printr_helper.php
Created December 27, 2014 01:20
Helper function for printr()
/**
* Prints out array in human readable form, just a helper function
* @param array $arr The wanted array to be printed
* @return array The array in human readable form
*/
function printr_func($arr)
{
echo "<pre class='printr'><code>";
print_r($arr);
echo "</code></pre>";
@matesnippets
matesnippets / gist:9b4db275f9cb2e644a05
Created January 10, 2015 19:57
PHP, Keep numbers in a string
$str = preg_replace('/[^0-9.]+/', '', $str);
@matesnippets
matesnippets / bash-color.sh
Created January 16, 2015 16:18
Bash, Color function
# Colors
color_echo() {
# Parameters
local message=$1
local color=${2-'default'}
local attribute=${3-'none'}
# Other vars
local color_type='3'
local color_code='0'
@matesnippets
matesnippets / yepnope-prefix-path.js
Created February 14, 2015 12:41
Here's an example how to define prefixes for YepNope.
/*
* Get paths to the js files to use in yepnope
* window.thePath is defined in footer
*/
(function(yepnope) {
// Domain name
// var domain = window.location.host,
// origin = window.location.origin;
@matesnippets
matesnippets / browser-detect.js
Created February 14, 2015 12:54
JavaScript, detect browser
/**
* Detect a browser
* http://stackoverflow.com/questions/13478303/correct-way-to-use-modernizr-to-detect-ie
*/
var BrowserDetect =
{
init: function ()
{
this.browser = this.searchString(this.dataBrowser) || "Other";
this.version = this.searchVersion(navigator.userAgent) || this.searchVersion(navigator.appVersion) || "Unknown";
@matesnippets
matesnippets / classes.js
Created February 23, 2015 20:01
JavaScript, Add, remove, and toggle a class name in a given DOM element with pure JavaScript
define(function() {
return {
/**
* Adds a class name to an element
* @param {element} el The target element
* @param {string} clazz The class names wanted to add
*/
add: function addClass(el, clazz) {
var cn = el.className;
// Test for existance
@matesnippets
matesnippets / slugger.php
Created March 9, 2015 14:56
WordPress, get custom post type slug
/**
* Gets a slug of a custom posty
* @param string $post_type The post type wanted
* @return string Slug for the post type
*/
function slugger($post_type)
{
if ($post_type) {
$post_type_data = get_post_type_object($post_type);
$post_type_slug = $post_type_data->rewrite['slug'];