Skip to content

Instantly share code, notes, and snippets.

@cferdinandi
cferdinandi / get-siblings.js
Last active August 29, 2021 15:56
A simple script to get all siblings of an element.
/**
* Get siblings of an element
* @param {Element} elem
* @return {Object}
*/
var getSiblings = function (elem) {
var siblings = [];
var sibling = elem.parentNode.firstChild;
var skipMe = elem;
for ( ; sibling; sibling = sibling.nextSibling )
@cferdinandi
cferdinandi / font-face-feature-test.js
Last active September 16, 2017 10:39
A simple feature test to detect @font-face support. Include the file in your <head> element. Adds a `.font-face` class to the <html> element when `@font-face` and the `:before` psuedo selector are supported. Via Paul Irish, Diego Perini, and Mat “Wilto” Marquis.
/**
* Test for @font-face support
* @author Filament Group
* @link https://github.com/filamentgroup/face-off
* @param {Element} win
* @param {Null} undefined
* @return {Boolean}
*/
var isFontFaceSupported = (function( win, undefined ) {
@cferdinandi
cferdinandi / pseudo-selector-feature-test.js
Last active December 21, 2015 07:19
A simple feature test to detect pseudo selector support. Via Paul Irish and Diego Perini: https://gist.github.com/paulirish/441842
/**
* Test for pseudo-selector support
* @author Paul Irish
* @link https://gist.github.com/paulirish/441842
* @param {String} selector Pseudo-selector to test
* @return {Boolean}
*/
function selectorSupported(selector){
var support, link, sheet, doc = document,
@cferdinandi
cferdinandi / iife.js
Last active December 7, 2017 06:59
An IIFE wrapper for JS functions...
;(function (window, document, undefined) {
'use strict';
// Code goes here...
})(window, document);
@cferdinandi
cferdinandi / live-reload.js
Created September 4, 2013 23:51
Live reload script for Sublime Text 2.
<script>document.write('<script src="http://' + (location.host || 'localhost')
.split(':')[0] + ':35729/livereload.js?snipver=1"></' + 'script>')</script>
@cferdinandi
cferdinandi / wordpress-url-helpers.php
Last active December 23, 2015 08:29
Simple helper functions to get, sanitize, and process URLs in wordpress. (Find and replace `_s_` with your own namespacing.)
<?php
// Get and sanitize the current URL
function _s_get_url() {
// Get URL
$url = @( $_SERVER['HTTPS'] != 'on' ) ? 'http://' . $_SERVER['SERVER_NAME'] : 'https://' . $_SERVER['SERVER_NAME'];
// $url .= ( $_SERVER['SERVER_PORT'] !== 80 ) ? ":" . $_SERVER['SERVER_PORT'] : ''; // Uncomment to add port number
$url .= $_SERVER['REQUEST_URI'];
@cferdinandi
cferdinandi / wordpress-alert-messages.php
Last active August 13, 2019 17:32
Simple functions to set and retrieve alert messages in a browser session in WordPress. (Find and replace `_s_` with your own namespacing.)
<?php
// Start browser session (to store alert messages)
function _s_start_browser_session() {
if(!isset($_SESSION)) {
session_start();
}
}
add_action('init', '_s_start_browser_session');
@cferdinandi
cferdinandi / has-characters.php
Last active January 7, 2024 11:58
Test strings for letters, numbers, and special characters. Returns true if they exist, false if they don't. Forked from http://stackoverflow.com/a/9588010/1293256
<?php
// Does string contain letters?
function _s_has_letters( $string ) {
return preg_match( '/[a-zA-Z]/', $string );
}
// Does string contain numbers?
function _s_has_numbers( $string ) {
return preg_match( '/\d/', $string );

[abc] A single character: a, b or c
[^abc] Any single character but a, b, or c
[a-z] Any single character in the range a-z
[a-zA-Z] Any single character in the range a-z or A-Z
^ Start of line
$ End of line
\A Start of string
\z End of string
. Any single character
\s Any whitespace character

@cferdinandi
cferdinandi / wordpress-translations.php
Last active December 26, 2015 03:19
A few simple translation functions for WordPress.
<!-- Translate -->
<?php __( '', 'kraken' ) ?>
<!-- Translate and echo -->
<?php _e( '', 'kraken' ) ?>
<!-- Translate with a variable -->
<?php printf( __( 'Thing one %s', 'kraken' ), $thing1 ); ?>
<!-- Translate with multiple variables -->