Skip to content

Instantly share code, notes, and snippets.

@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 / 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'];
@matesnippets
matesnippets / back-link.php
Created March 9, 2015 14:58
WordPress, intelligent back link to previous page
@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 / 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 / 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 / cm_svg_get.php
Created March 9, 2015 15:24
PHP, WordPress, SVG image getter, embed SVG straight to the page, no img tag needed and keeps templated nice and tidy.
/**
* Get SVG file as raw SVG, not img tag
* @param string $path Path to images direcotry
* @param string $file The filename, file extension (.svg) can be omitted
* @return string The contents of the wanted SVG file
*/
function cm_svg_get($path = 'images', $file = '')
{
// $file = $file.".svg";
$svg = ".svg";
@matesnippets
matesnippets / printr_func.php
Created March 9, 2015 15:27
PHP, print_r() helper function. Prints arrays out in a nice, formatted, human readable way.
/**
* 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 / class_excerpt.php
Created March 9, 2015 15:29
PHP, WordPress, With this you can give excerpt a custom class, nice when BEMming.
/**
* Adds a custom class to excerpt `p` tah
* @param string $class The wanted new class
* @return string `p` tag with a class and the content of the excerpt
*/
function cm_excerpt($class)
{
$excerpt = strip_tags(get_the_excerpt());
echo '<p class="' . $class . '">' . $excerpt . '</p>';
}