Limit WordPress Image Upload Size

function ter_limit_image_uploads($file){
$error_message = 'KB is to large. Please reduce the file size of your image to ' . TER_MAX_IMAGE_SIZE_KB . 'KB or less. Hosting space is limited and uploading large images will slow down your page loads. Use a tool such as http://toki-woki.net/p/Shrink-O-Matic/ to help you to resize your images. Target size: Width 1024px, file size 100KB-200KB';
$size_in_kb = $file['size'] / 1024;
if(preg_match('/image/',$file['type'])) if($size_in_kb > TER_MAX_IMAGE_SIZE_KB) $file['error'] = round($size_in_kb,2) . $error_message;
return $file;
}
add_filter('wp_handle_upload_prefilter','ter_limit_image_uploads'); //Limit uploaded image size

Sort multidimensional array

<?php
$people = array(
array("name"=>"Bob","age"=>8,"colour"=>"red"),
array("name"=>"Greg","age"=>12,"colour"=>"blue"),
array("name"=>"Andy","age"=>5,"colour"=>"purple"));

var_dump($people);

$sortArray = array();

foreach($people as $person){
foreach($person as $key=>$value){
if(!isset($sortArray[$key])){
$sortArray[$key] = array();
}
$sortArray[$key][] = $value;
}
}

$orderby = "name"; //change this to whatever key you want from the array

array_multisort($sortArray[$orderby],SORT_DESC,$people);

var_dump($people);
?>

Facebook Like To View

function parsePageSignedRequest() {
if (isset($_REQUEST['signed_request'])) {
$encoded_sig = null;
$payload = null;
list($encoded_sig, $payload) = explode('.', $_REQUEST['signed_request'], 2);
$sig = base64_decode(strtr($encoded_sig, '-_', '+/'));
$data = json_decode(base64_decode(strtr($payload, '-_', '+/'), true));
return $data;
}
return false;
}

if($signed_request = parsePageSignedRequest()) {
if($signed_request->page->liked) {
echo "This content is for Fans only!";
} else {
echo "Please click on the Like button to view this tab!";
}
}

/* Change YOUR_APP_ID and YOUR_APP_ID */
/* YOUR_APP_ID = ID of App you want to link to */
/* YOUR_URL = URL of app you are wanting to link to */
https://www.facebook.com/dialog/pagetab?app_id=YOUR_APP_ID &display=popup&next=YOUR_URL

Minify Stylesheets Into One Request

<?php
/*
* This function will loop over some stylesheets and minify them into one request
* Place in <head>
*/
function load_head_styles() {
$stylesheets = array(
'bootstrap' => array(
'location' => '/css/bootstrap.css'
),
'theme' => array(
'location' => '/css/custom.css'
)
);

foreach ( $stylesheets as $stylesheet ) :
$css_to_minify[] = $stylesheet['location'];
endforeach;

?><link rel="stylesheet" type="text/css" href="/min/index.php?f=<?php echo implode( ',', $css_to_minify ); ?>"><?php

}
?>

Reference calling class

<?php

class CallingClass{
public $instance_class;
public function __construct(){
$this->instance_class = new InstanceClass($this);
}
public function show_new_data($data){
echo 'This in the data set in an instance created by this class: ' . $data;
}
}

class InstanceClass{
protected $_calling_class;
public function __construct(CallingClass $calling_class){
$this->_calling_class = $calling_class;
}
public function add_data($data){
$this->_calling_class->show_new_data($data);
}
}

$test = new CallingClass();
$test->instance_class->add_data('Hello World');

?>

Observer pattern

<?php

/* Observer Interface */
interface Observer{
function register_change($subject,$args);
}

/* Observable Interface */
interface Observable{
function add_observer($observer);
}

/* Inbox Notifier */
class InboxNotifier implements Observer{
public function register_change($subject,$args){
echo "A new message has been added to your inbox: <br>$args";
}
}

/* Inbox Subject */
class MyInbox implements Observable{
private $_observers = array();
public function add_observer($observer){
$this->_observers []= $observer;
}
public function add_message($message){
foreach($this->_observers as $observer) $observer->register_change($this,$message);
}
}

$my_inbox = new MyInbox();
$my_inbox->add_observer(new InboxNotifier());
$my_inbox->add_message('This is a test message');
?>