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 / classList.js
Created January 14, 2015 15:44
JavaScript, classList shim
/*
* classList.js: Cross-browser full element.classList implementation.
* 2014-07-23
*
* By Eli Grey, http://eligrey.com
* Public Domain.
* NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.
*/
/*global self, document, DOMException */
@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 / hider-shower.js
Last active September 15, 2015 06:44
JavaSript, Toggle a panel
'use strict';
define(function() {
/**
* Toggles a drawer panel
*
* Click a button and a drawer is shown, click elswhere and it's closed,
* open it when element inside the drawer is focused with a keyboard.
*
* @param {string} trigger ID, class, or element name to the button that toggles the drawer