Skip to content

Instantly share code, notes, and snippets.

View wpscholar's full-sized avatar
😀
Happy

Micah Wood wpscholar

😀
Happy
View GitHub Profile
@wpscholar
wpscholar / jquery.positionRelativeTo.js
Last active September 20, 2017 23:27
A jQuery plugin that will position an element relative to another element, regardles of whether or not they share the same parent in the DOM.
(function ( $ ) {
/**
* A jQuery plugin that will position an element relative to another element, regardles of whether or not they share the
* same parent in the DOM.
*
* Note: This must be called within a $(document).ready() call to work properly. If loading images in the element
* that aren't specifically sized via CSS, it may be necessary to call this within a $(window).load() call
* depending on the positioning used.
*
@wpscholar
wpscholar / jquery.toggleElement.js
Last active September 20, 2017 23:27
A jQuery plugin that will toggle the display of an element when another element is clicked. When the toggled element is being displayed, clicking on the initiating element or any other element that is not the toggled element or its children will cause the toggled element to be hidden. Works on touchscreens.
(function( $ ) {
/**
* A jQuery plugin that will toggle the display of an element when another element is clicked.
* When the toggled element is being displayed, clicking on the initiating element or any other element that
* is not the toggled element or its children will cause the toggled element to be hidden.
*
* @param $el
* @returns {*}
*/
@wpscholar
wpscholar / jquery.iosTapOut.js
Created May 8, 2013 15:17
jQuery plugin to force a blur event in iOS when a user taps out of an input.
$.fn.iosTapOut = function() {
return this.each( function() {
var $this = $(this);
var canTouch = 'ontouchstart' in document.documentElement;
if( canTouch ) {
$this.data( 'iosTapOut.active', false );
@wpscholar
wpscholar / get-url.php
Last active December 17, 2015 03:19
A function for use within WordPress themes or plugins that fetches the current URL without the query string.
<?php
/**
* Helper function fetches current URL less the query string
*
* @return string
*/
function get_url(){
global $wp;
$url = parse_url( add_query_arg( $wp->query_string, '', home_url( $wp->request ) ) );
@wpscholar
wpscholar / http-vhosts.conf
Created June 26, 2013 15:43
Simplify local development with dynamic VirtualHosts
NameVirtualHost *:80
<VirtualHost *:80>
ServerName dev
ServerAlias *.dev
VirtualDocumentRoot "/Users/home/Sites/%-2/public_html"
<Directory "/Users/home/Sites/">
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
@wpscholar
wpscholar / local-time.php
Last active September 20, 2017 23:26
Properly fetch the local date / time in WordPress based on a Unix timestamp.
<?php
/**
* Fetch the local date/time in WordPress based on a Unix timestamp.
*
* @param int $timestamp
* @param string $format
* @return string
*/
function get_wp_local_date_time( $timestamp, $format = '' ) {
@wpscholar
wpscholar / style.css
Created July 25, 2013 12:22
Flexible content area with fixed width sidebar.
.content {
float: left;
margin-right: 300px; /* Must match sidebar width */
padding-right: 20px; /* Gutter width */
}
.sidebar {
float: right;
width: 300px;
margin-left: -100%; /* Won't work without this */
}
@wpscholar
wpscholar / attribute-string.php
Created July 27, 2013 11:58
Convert an associative array of attributes to a string suitable for use in HTML.
<?php
function get_attribute_string(array $atts ) {
foreach( $atts as $key => &$value ) {
$value = "{$key}=\"{$value}\"";
}
return join( ' ' , $atts );
}
@wpscholar
wpscholar / get-post-types-supporting.php
Last active September 4, 2016 08:52
A custom WordPress function to fetch a list of registered post types that support a specific feature.
<?php
/**
* Get a list of post types that support a specific feature.
*
* @param $feature
* @return array
*/
function get_post_types_supporting( $feature ) {
global $_wp_post_type_features;
@wpscholar
wpscholar / recursive-multidimensional-array-search-by-key.php
Created September 28, 2013 22:53
Recursively traverses a multidimensional array in search of a specific key and returns an array containing the keys that correspond to the path of the first found instance, or an empty array on failure.
<?php
/**
* Recursively traverses a multidimensional array in search of a specific key and returns an
* array containing the keys that correspond to the path of the first found instance, or an
* empty array on failure.
*/
function recursive_multidimensional_array_search_by_key( $key, array $data, $stack = array() ) {
if( array_key_exists( $key, $data ) ) {
array_push( $stack, $key );