Skip to content

Instantly share code, notes, and snippets.

View martynchamberlin's full-sized avatar

Martyn Chamberlin martynchamberlin

View GitHub Profile
@martynchamberlin
martynchamberlin / RandomNumber.java
Created February 7, 2014 05:28
Verbose Java. This is the first Java program I've written that actually took me a few hours to write and took some thought. It's probably a lot more verbose in length than it needs to be — in fact, I know this for a fact. I hope some day to review it and smile at how bad a programmer I was. The main method is stored in UX.java. That name only ma…
/**
* Martyn Chamberlin
*/
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Random;
/**
* In true purist fashion, every field in this class is set to private, and more
@martynchamberlin
martynchamberlin / functions.php
Last active August 29, 2015 13:58
Move data from custom Wishlist Member registration fields to WP User Meta
<?php
/**
* Just paste this code into your theme's functions.php. When the user submits the form, the
* input fields (specified by $fields array) goes into a bunch of cookies. Once the person is
* actually logged in and has a wp_users row associated with then, we then take this cookie
* data and associated it with their account via the user_meta API. Lastly we delete the cookies.
*/
function move_data_from_wl_to_wp()
{
$fields = array( 'state', 'county', 'address1', 'address2', 'city', 'zip' );
@martynchamberlin
martynchamberlin / eval.php
Last active August 29, 2015 13:59
Simple PHP evaluluator
<?php
/**
* Often times in development, I find it convenient to test something out in
* the rough before I actually implement it in the software I'm working on.
* Usually this is to get the parameters and "ropes" of a built-in function
* into my head before testing it in a real (i.e., more moving parts, and
* therefore more difficult to debug) environment. I do this in whatever
* language I happen to be working in — whether that be Java, C#, or PHP.
*
* In terminal, you can run `php -a` to simulate a quick PHP test. That is
@martynchamberlin
martynchamberlin / force-http-wordpress.php
Created April 23, 2014 22:22
In case you need to force HTTPS on every single page of a WordPress site, here's how.
<?php
$using_ssl = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' || $_SERVER['SERVER_PORT'] == 443;
add_action('wp', 'check_ssl');
function check_ssl()
{
// All pages must be https. Redirect to https if not already
if (! $using_ssl)
{
header('HTTP/1.1 301 Moved Permanently');
@martynchamberlin
martynchamberlin / DOM.html
Last active August 29, 2015 14:00
Sticky footer for complex case scenarios where other intruding DOM elements keep us from being able to use Ryan Fait's footer
<html>
<head>
</head>
<body>
<!-- the fact that this main block has an ID of #wrap has no actual bearing on this plugin -->
<div id="wrap">Main site content goes here, though the div doesn't
really have to close before footer begins. That doesn't matter either
way, which is what I think makes this solution so beautiful.
</div>
<footer>Copyright 2014</footer>
@martynchamberlin
martynchamberlin / addClass.js
Created June 16, 2014 15:00
Sometimes when content is coming out of a CMS it's faster to target an element by just adding a unique class via jQuery. This does that for you with any element(s) of your choosing. See sample function call at the bottom.
jQuery(document).ready(function( $ )
{
/**
* @param obj - array of strings. The element name to be targeted
*/
function addClasses( objArray )
{
for ( i = 0; i < objArray.length; i++ )
{
// Save CPU by storing this in local variable
@martynchamberlin
martynchamberlin / ecological-validity.swift
Last active August 29, 2015 14:03
Calculates the mathematical probability of ecological validity of a given experiment, where the Y axis is the percentage of probability and each data point in the graph is a new independent variable being introduced to the experiment. As more independent variables are introduced, the accuracy exponentially diminishes. Needless to say, you need t…
// Playground - noun: a place where people can play
import Cocoa
var i: Int = 0
func power(#x :Int, #y :Int) -> Int {
if(y == 0) {
return 1;
}
@martynchamberlin
martynchamberlin / example_of_static_in_php.php
Last active August 29, 2015 14:03
PHP overloads the `static` keyword. In instance variables, it means the classical OOP usage. In local variables (stack instead of heap) it means something else. This code demonstrates that something else.
<?php
function get_random_num( $start = 1, $finish = 2 )
{
static $rand;
if ( ! isset( $rand ) )
{
$rand = rand( $start, $finish );
}
return $rand;
@martynchamberlin
martynchamberlin / functions.php
Created August 27, 2014 19:59
Remove White Space Between <li> Elements in a Genesis Menu
<?php
/**
* Having `display: inline-block` works great as long as there isn't white space.
*/
add_filter('genesis_do_nav', 'remove_whitespace');
function remove_whitespace( $str )
{
$str = str_replace( "\r\n", "", $str );
$str = str_replace( "\r", "", $str );
@martynchamberlin
martynchamberlin / centerVertically.js
Created September 16, 2014 02:32
centerVertically.js—a simple extension to the jQuery library that allows you to vertically center most anything.
/**
* centerVertically() takes a given DOM element and centers
* it vertically within the parent element. If the height
* of the browser is less than this element's height, then
* the original margin relative to top is restored until
* the window gets larger again.
*
* Note that this function extends the jQuery library and
* therefore requires it before this function can be
* defined.