Skip to content

Instantly share code, notes, and snippets.

/**
* Works in Chrome, Firefox, MSIE, Opera and Safari.
*/
function onDOMContentLoaded( event ) {
if( event ) {
document.removeEventListener( event.type, onDOMContentLoaded, false );
}
document.onreadystatechange = null;
// For testing purposes.
alert( "DOM Content Loaded" );
@dalethedeveloper
dalethedeveloper / gist:965036
Created May 10, 2011 18:18
Wordpress + Facebook Done Right: Load FBConnect asynchronously for XFBML and handle the channelUrl
/**
* A class-based approach to bring in Facbook Connect asynchronously on every
* frontend page load. Also handles side-loading channelURL when using
* Facebook's FB.init() method.
*
* This fits nicely in any functions.php, or can be dropped in to a standalone
* php file to use as a plugin.
*
* @link http://developers.facebook.com/docs/reference/javascript/FB.init/
*/
@dalethedeveloper
dalethedeveloper / gist:966848
Created May 11, 2011 16:47
Reorder a PHP array, moving items up or down
<?php
/*
A quick set of functions to move items in a non-associative array
up or down by one, shifting the items around it appropriately.
Original usage was to for a set of UP and DOWN buttons to
manipulate the order of an array of items stored in Wordpress option.
*/
$a = array('a','b','c','d','e');
@dalethedeveloper
dalethedeveloper / gist:968926
Created May 12, 2011 16:52
Encrypt a WordPress Option using MySQL ENCRYPT()
/*
An example of using MySQL's ENCRYPT() and DECRYPT() functions to store
sensitive data in a Wordpress Option. In this case, a password.
This only provides a bare amount of security as your Key is likely stored somewhere
else in your code or database. Basically, its not plaintext.
http://dev.mysql.com/doc/refman/5.5/en/encryption-functions.html
*/
@dalethedeveloper
dalethedeveloper / gist:1042934
Created June 23, 2011 16:35
Backup bash script for Webserver DB + Site Files
#!/bin/bash
# http://www.ivorde.ro/How_to_backup_all_mysql_databases_with_one_command-51.html
# http://bash.cyberciti.biz/backup/backup-mysql-database-server-2/
# http://sgowtham.net/blog/2008/04/04/backing-up-and-restoring-mysql-databases/
# Requires: http://s3tools.org/s3cmd
MyUSER="YOUR-USERNAME"
MyPASS="YOUR-PASSWORD"
MyHOST="localhost"
@dalethedeveloper
dalethedeveloper / gist:1093962
Created July 19, 2011 23:01
Mimic a static HTML file from Wordpress using functions.php
/* When an html page like URL is accessed, return some static content,
In this case, a verification page for Google Apps
In the current theme functions.php:
*/
if( !class_exists('google_verify') ) {
class google_verify {
public function __construct() {
add_filter('rewrite_rules_array', array($this,'gv_rewrite') );
if( !is_admin() ) {
@dalethedeveloper
dalethedeveloper / gist:1123968
Created August 3, 2011 22:20
Timestamp to a plain words description of elapsed delta (great for Twitter timestamps)
// Adapted from jQuery TimeAgo plugin:
// https://github.com/rmm5t/jquery-timeago/blob/master/jquery.timeago.js
// Update, check out performance: http://jsperf.com/twitter-relative-time-parsing
function timetowords(timestamp) {
var words = {
"seconds": "less than a minute",
"minute": "about a minute",
"minutes": "%d minutes",
@dalethedeveloper
dalethedeveloper / gist:1126105
Created August 4, 2011 20:07
Minimalist post-load of Twitter Statuses to a page using jQuery (with bonus relative time description + Wordpress wraps!)
jQuery(document).ready(function($) {
$.getJSON('http://twitter.com/statuses/user_timeline/Shitmydadsays.json?count=4&callback=?', function(data){
var relative_time = function(datetime) {
var delta = parseInt((Date.now() - Date.parse(datetime)) / 1000, 10);
var r = '';
if (delta < 60) {
r = delta + ' seconds ago';
} else if (delta < 120) {
r = 'A minute ago';
} else if (delta < (45 * 60)) {
@dalethedeveloper
dalethedeveloper / gist:1222205
Created September 16, 2011 14:11
Implementing rel="next" and rel="prev" on Wordpress
/*
This will work on WP 2.7.0 on up on the commonly used get_xxxxxxx_posts_link call.
From Google:
http://googlewebmastercentral.blogspot.com/2011/09/pagination-with-relnext-and-relprev.html
*/
add_filter('next_posts_link_attributes', create_function('$attr','return $attr.\' rel="next" \';'));
add_filter('previous_posts_link_attributes', create_function('$attr','return $attr.\' rel="prev" \';') );
@dalethedeveloper
dalethedeveloper / gist:1246351
Created September 27, 2011 21:48
Wordpress functions.php snippet to add Google Analytics tracking to wp_nav_menu with Yoast's plugin
/*
For use in functions.php in combination with:
http://wordpress.org/extend/plugins/google-analytics-for-wordpress/
*/
add_filter('wp_nav_menu', 'menu_ga_tracking', 99);
function menu_ga_tracking($menu) {
if( class_exists('GA_Filter') and yoast_ga_do_tracking() ) {
$menu = preg_replace_callback(