Skip to content

Instantly share code, notes, and snippets.

View kythin's full-sized avatar

Chris Samios kythin

View GitHub Profile
@kythin
kythin / daylist
Created February 25, 2012 10:15
HTML Select lists of Years, Months, Days
<select name="dob-day" class="datefield day">
<option value="">Day</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
@kythin
kythin / GA_RecordOutboundLink.js
Created April 3, 2012 06:04
Google Analytics - Track link click as action and then open in new window
@kythin
kythin / wp_get_cat.php
Created June 1, 2012 05:11
Wordpress quick function to get current post's category id(s)
function getCurrentCatID(){
//Probably only works inside the_loop();
global $wp_query;
if(is_category() || is_single()){
$cat_ID = get_query_var('cat');
}
return $cat_ID;
}
@kythin
kythin / wp_show_some_images_from_uploads.php
Created June 22, 2012 00:52
Grabs a directory of images from the wp uploads folder and displays them, also using their filenames for ordering and alts/titles.
<?php
/* Chris' magical logo display code.
* This will display all images in the uploads/logos directory, and use their filenames for the alt's and titles.
* Also allows file ordering by using numbers at the start of the filename, optionally followed by an underscore.
* Hyphens will be converted to spaces in the alt and title. Make sure to use the right case in the filename as you want
* in the alt & title.
* e.g. 0_Company.php => Company
* 05_Company-Name => Company Name
* 05_logo-Company-Name => Company Name
@kythin
kythin / twitter-oauth-quickstart.php
Created June 25, 2012 23:12
Twitter OAuth Quickstart
<?php
/*
* uses tmhOauth from https://github.com/themattharris/tmhOAuth/
* and is mostly based on the oauth flow example from that library (thanks matt!)
*
* also need your own twitter api key / secret, and make sure it's got write access
* if you want to publish posts (extra step when making your api key on twitter)
*
*
*/
@kythin
kythin / wp-fix-meta-serialization.php
Created June 25, 2012 23:31
Fix wordpress serialized variables in custom meta data when running a migration
<?php
/*
* Wordpress postmeta find&replace code
*
* This file should be run on your source database to echo out update statements to run on your migrated database.
* It is to address the issue of when doing a find/replace on an sql file during a wordpress migration, that breaks
* any content changed within wordpress serialized meta values, particularly if you have custom post types etc.
*
* Do your normal find/replace over the exported sql, import it into your new database, then run this code and copy the resulting
* update SQL into your new database to fix it.
@kythin
kythin / php_js_redirect.php
Created November 29, 2012 04:55
PHP or JS Redirect with optional timeout
<?php
/*
* Handy redirect function.
* Takes one URL parameter that can be relative or absolute.
* Optionally also takes timeout in seconds.
* Returns null.
* If headers have been sent, and no timeout of 0 specified, will do a PHP redirect.
* Otherwise, it will do a javascript redirect.
*/
@kythin
kythin / slugify.php
Created May 6, 2013 04:35
I forget now where I got this, probably stack overflow somewhere, but it's been in my library of common functions for a while. IIRC it was based on the wordpress slugify or maybe drupal or something.
function slugify($text) {
// replace non letter or digits by -
$text = preg_replace('~[^\\pL\d]+~u', '-', $text);
// trim
$text = trim($text, '-');
// transliterate
$text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
@kythin
kythin / array_flip.php
Created July 10, 2013 23:53
Beautifully simple columns to rows array flip in PHP. I didn't write this one, I've done this a hundred times for quick and dirty CSV processing scripts, but never kept nice clean code like this. Taken from http://stackoverflow.com/questions/2221476/php-how-to-flip-the-rows-and-columns-of-a-2d-array
function flip($arr) {
$out = array();
foreach ($arr as $key => $subarr)
{
foreach ($subarr as $subkey => $subvalue)
{
$out[$subkey][$key] = $subvalue;
}
}
@kythin
kythin / force_ssl.php
Created July 23, 2013 23:22
Force a page to use HTTPS using this function.
function forceSSL() {
if($_SERVER["HTTPS"] != "on") {
header("HTTP/1.1 301 Moved Permanently");
header("Location: https://" . $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"]);
exit();
}
}