Skip to content

Instantly share code, notes, and snippets.

@matesnippets
matesnippets / lazyload-attachment.php
Created March 9, 2015 15:13
WordPress, PHP, Get lazyload friendly attachement image based on attachment ID
/**
* Gets an attachement image as lazy loaded img based on ID
* @param string $size Thumbnail, medium, large, original, or {custom size}
* @param string $class CSS class name for the img tag
* @param integer $attachment_id ID of the attachment
* @param string $attachment_id The custom src atrribute to use, default to data-echo
* @return string Lazyload friendly HTML img tag
*/
function get_attachemt_lazy($size, $class, $attachment_id = "", $src_attr = "data-echo")
{
@matesnippets
matesnippets / trucator.php
Created March 9, 2015 15:00
PHP, Truncate string to a given length
/**
* Cut string to lenght and add prefix
* @param string $string The string to truncate
* @param int $length The amount of characters wanted
* @param string $prefix The prefix, default ...
* @return string The modified string
*/
function str_truncater($string, $length, $prefix = '...')
{
return strlen($string) > $length ? substr($string, 0, $length).$prefix : $string;
@matesnippets
matesnippets / url-stripper.php
Created March 9, 2015 14:59
PHP, Strip http, https, and www. from URL
/**
* Strips http:// and https:// and www. and the trailing slash / from URL
* @param string $url The URL to be processed
* @return string Modified URL
*/
function strip_crap_from_url($url)
{
return preg_replace('/https?:\/\/|www.|\/$/', '', $url);
}
@matesnippets
matesnippets / back-link.php
Created March 9, 2015 14:58
WordPress, intelligent back link to previous page
@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'];
@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 / closest.js
Created February 23, 2015 20:00
JavaScript, Get closest element by class. Mimics the jQuery's `.closest()` with pure JavaScript.
define(function() {
/**
* Get the closest element of a given element by class
*
* Take an element (the firt param), and traverse the DOM upward from it
* untill it hits the element with a given class name (second parameter).
* This mimics jquery's `.closest()`.
*
* @param {element} el The element to start from
* @param {string} clazz The class name
@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
@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 / 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;