Skip to content

Instantly share code, notes, and snippets.

View dave1010's full-sized avatar

Dave Hulbert dave1010

View GitHub Profile
@dave1010
dave1010 / jquery.log.js
Created February 16, 2012 11:45
jQuery console logger
/*!
* jQuery Console Logger v1.0
*
* Copyright 2012, Dave Hulbert
* Dual licensed under the MIT or GPL Version 2 licenses.
*
* Usage: add ".log()" inline in a jQuery chain
* $('div').parent().parent().log().find('span').next().log();
*/
jQuery.fn.log = function() {
@dave1010
dave1010 / htaccess
Created December 14, 2011 13:07
HTTP Status Cats Apache (htaccess) config
# HTTP Status Cats
# Apache (htaccess) config created by @dave1010
# Licensed CC BY 2.0
# Images CC BY 2.0, from GirlieMac's photostream:
# http://www.flickr.com/photos/girliemac/sets/72157628409467125/with/6508023065/
# Usage: copy save this file as .htaccess or add it to your httpd.conf
ErrorDocument 404 '<a href="http://www.flickr.com/photos/girliemac/6508022985/" title="404 - Not Found by GirlieMac, on Flickr"><img src="http://farm8.staticflickr.com/7172/6508022985_b22200ced0.jpg" width="500" height="400" alt="404 - Not Found"></a>'
@dave1010
dave1010 / pre-commit
Created October 28, 2011 12:31
pre-commit PHP link and check for merge conflicts
#!/bin/bash
conflicts=`git diff --cached --name-only -S'<<<<<<'`
for i in `git diff --cached --name-only | grep '.php'`;
do
thisphperror=`php -l $i | grep -v 'No syntax errors detected in'`
if [ -n "$thisphperror" ]; then
echo "PHP errors added in file: " $i
@dave1010
dave1010 / jquery.commentToHTML.js
Created October 10, 2011 15:40
jQuery plugin: set the HTML of an element to be the contents of its first comment
/*global $ */
/**
* Replace an element's HTML with the content of its first comment
*/
$.fn.commentToHTML = function() {
var d = 'extracted-comments';
return this.each(function(i, el) {
var $el = $(this);
@dave1010
dave1010 / php-regex-router.php
Created September 13, 2011 15:46
php-regex-router.php
<?php
// dangerously simple PHP regular expression URL router
// requires a mod_rewrite like "RewriteRule . /index.php [L]"
function get($url, $callback) {
$matches = array();
if (preg_match('~' . $url . '~', $_SERVER['REQUEST_URI'], $matches)) {
echo call_user_func_array($callback, $matches);
die();
}
@dave1010
dave1010 / hnpanel.js
Created August 23, 2011 11:51
the code from @chetan51 's hnpanel bookmarklet
(function(e, a, g, h, f, c, b, d) {
if (! (f = e.jQuery) || g > f.fn.jquery || h(f)) {
c = a.createElement("script");
c.type = "text/javascript";
c.src = "http://ajax.googleapis.com/ajax/libs/jquery/" + g + "/jquery.min.js";
c.onload = c.onreadystatechange = function() {
if (!b && (!(d = this.readyState) || d == "loaded" || d == "complete")) {
h((f = e.jQuery).noConflict(1), b = 1);
f(c).remove()
}
@dave1010
dave1010 / 00-the-wordpress-way.md
Created August 18, 2011 09:21
The WordPress Way - The 10 rules of WordPress development

The WordPress Way

A tongue-in-cheek look at coding standards in the WordPress core and the average WordPress plugin.

  1. # Declare variables global - in case they're going to be used again
  2. All function/method parameters should be strings (e.g. 'yes'/'no') - for clarity
  3. Functions and methods should return mixed types
  4. No need to separate PHP logic from HTML, JS or CSS
  5. Don't worry about PHP Notices - they're not important
@dave1010
dave1010 / HtmlNode.php
Created August 9, 2011 14:02
HtmlNode.php - generate HTML from PHP
<?php
/**
* Quick and dirty HTML generation for PHP
*/
class HtmlNode {
public $element = 'div';
public $content = null;
public $attrs = array();
public function __construct($element=null, $content=null, $attrs=null, $closing=null, $shortclosing=' /') {
@dave1010
dave1010 / firebug-to-iframe.js
Created June 13, 2011 11:02
Make Firebug's commandline execute commands in an iframe
cd($$('iframe')[0].contentWindow)
@dave1010
dave1010 / to-string.php
Created June 10, 2011 15:02
How to throw exceptions in a PHP class' __toString method
<?php
class MyClass {
public function __toString() {
try {
return $this->render();
} catch(Exception $e) {
// the __toString method isn't allowed to throw exceptions
// so we turn them into an error instead
trigger_error($e->getMessage() . "\n" . $e->getTraceAsString(), E_USER_ERROR);