Skip to content

Instantly share code, notes, and snippets.

@pospi
pospi / hierarchy-sort.php
Last active December 22, 2015 10:19
Wordpress helper method for efficiently organising a hierarchical taxonomy into a hierarchical data structure. Similar code could be used to organise any hierarchical dataset.
<?php
/**
* Recursively sort an array of taxonomy terms hierarchically. Child categories will be
* placed under a 'children' member of their parent term.
*
* @param Array $cats taxonomy term objects to sort
* @param Array $into result array to put them in
* @param integer $parentId the current parent ID to put them in
*/
function sortHierarchicalTaxonomy(Array $cats, Array &$into, $parentId = 0)
@pospi
pospi / adminboxshuffle.class.php
Last active July 5, 2020 15:18
Wordpress edit page metaboxes reshuffler: moves 'author' and 'revisions' boxes to the sidebar, and places the excerpt above the editor. Excerpt hint text is configurable.
<?php
add_action('add_meta_boxes', array('AdminBoxShuffle', 'repositionDefaultMetaboxes'), 0);
class AdminBoxShuffle
{
const EXCERPT_HINTTEXT = 'This excerpt is shown at the top of your posts and will form the preview text on gateway pages.';
/**
* Reposition default metaboxes
@pospi
pospi / wpengine-cdnurl.php
Last active October 14, 2016 08:11
Premodify URLs on WPEngine to directly point to your CDN. Avoids issues some link generators have with HTTP redirects.
<?php
function wpe_noRedirectUrl($srcURL)
{
if (class_exists('WpeCommon')) {
global $wpe_netdna_domains;
static $cdn_domain;
if (!isset($cdn_domain)) {
$wpe_common = new WpeCommon();
if ($wpe_common->is_cdn_enabled()) {
@pospi
pospi / reset-admin-ui.js
Created September 13, 2013 06:56
Reset the Wordpress admin UI to its default state after failing some clientside validation upon saving.
$('#publishing-action')
.find('.spinner').hide().end()
.find('#publish').removeClass('button-primary-disabled');
$('#save-action')
.find('.spinner').hide().end()
.find('#save-post').removeClass('button-disabled');
@pospi
pospi / jquery.clickoutside.js
Created September 24, 2013 00:54
Very simple jQuery event for handling unfocusing of elements when clicking outside of them.
(function($) {
var WATCH_FOCUS_ON = $();
$.event.special['clickoutside'] = {
setup: function()
{
WATCH_FOCUS_ON = WATCH_FOCUS_ON.add( this );
// bind document handler if this is the first guy being bound
@pospi
pospi / charset-converter.php
Created September 24, 2013 01:03
Class for working with data to be used in latin-1 encoded HTML documents, or for normalising user input.
<?php
class CharsetConverter
{
private static $WORD_CHARS_MAP = array(
"\xE2\x80\x9A" => "&sbquo;",
"\xE2\x80\x9E" => "&bdquo;",
"\xE2\x80\x98" => "'",
"\xE2\x80\x99" => "'",
"\xE2\x80\x9C" => "\"",
@pospi
pospi / jquery-plugin-pattern.js
Created September 24, 2013 01:05
Boilerplate code for wrapping up a JavaScript object / class into a jQuery plugin
/**
* A jquery plugin wrapper for a pure JavaScript class
*
* This will automatically give us the following:
*
* - a constructor for our class using $('my.selector').myClass()
* - automatic method access to the class's instance functions via syntax
* $('my.selector').myClass('funcName', arg1, arg2, ...argN);
* - automatic property access of all the class's internal properties. Both this
* and the above can assist greatly with third-party code integration
@pospi
pospi / simple-date-range.js
Created September 24, 2013 01:09
Simple method for determining the number of periods between two dates
/**
* usage: date/time date/time ms->sec->min->hour->day
* getPeriodBetween('1 Jul 2011', '26 Oct 2010', 1000 * 60 * 60 * 24);
*
* = number of days between these two dates
*/
function getPeriodBetween(date1, date2, timescale)
{
return (new Date(date1) - new Date(date2)) / (timescale);
}
@pospi
pospi / placeholders.css
Last active December 23, 2015 18:59
Placeholder styles for all browsers. Note the opacity resets, as this is how the default placeholder styles are applied.
::-webkit-input-placeholder { /* WebKit browsers */
color: #F00;
opacity: 1;
}
:-moz-placeholder { /* Mozilla Firefox 4 to 18 */
color: #F00;
opacity: 1;
}
::-moz-placeholder { /* Mozilla Firefox 19+ */
color: #F00;
@pospi
pospi / fontsize-reset.less
Last active December 23, 2015 18:59
Setup base font size reliably with CSS. Creates any baseline font size you wish to base your em units off.
@BASE_FONT_SIZE = 16;
@BASE_LINE_HEIGHT = 1.5;
// consistent base font size & line height (16px)
html {
font-size: 100%;
*font-size: 16px;
line-height: @BASE_LINE_HEIGHT;
}