Skip to content

Instantly share code, notes, and snippets.

@joshhartman
joshhartman / dashboard-commander.php
Created January 27, 2011 23:26
Dashboard Commander
<?php
/*
Plugin Name: Dashboard Commander
Plugin URI: http://www.warpconduit.net/wordpress-plugins/dashboard-commander/
Description: Command your admin dashboard. Manage built-in widgets and dynamically registered widgets. Hide widgets depending upon user capabilities. Plugin is based upon Dashboard Heaven by Dave Kinkead.
Version: 1.0.1
Author: Josh Hartman
Author URI: http://www.warpconduit.net
License: GPL2
*/
@joshhartman
joshhartman / gist:835635
Created February 20, 2011 02:48
Days Ago Function
<?php
date_default_timezone_set('America/Chicago'); // Set this in your app's config
function getHowLongAgo($date, $display = array('Year', 'Month', 'Day', 'Hour', 'Minute', 'Second'), $ago = 'Ago')
{
$date = getdate(strtotime($date));
$current = getdate();
$p = array('year', 'mon', 'mday', 'hours', 'minutes', 'seconds');
$factor = array(0, 12, 30, 24, 60, 60);
for ($i = 0; $i < 6; $i++) {
@joshhartman
joshhartman / gist:835648
Created February 20, 2011 03:00
List Certain Types of Files in a Directory
<?php
$folder = opendir('.'); // Use '.' only if the PHP file is in the same folder as your files, otherwise use a relative or absolute path.
$file_types = array('jpg', 'jpeg', 'gif', 'png');
$file_list = array();
while ($file = readdir ($folder)) {
$parts = explode('.', $file);
$ext = array_pop($parts);
if(in_array($ext, $file_types)) {
array_push($file_list, $file);
}
@joshhartman
joshhartman / gist:836280
Created February 20, 2011 20:35
Convert Number to Ordinal Number with Suffix Text
<?php
date_default_timezone_set('America/Chicago');
// This function will take a number and add "th, st, nd, rd, th" after it. For example: echo ordinal(10); // outputs '10th'
function ordinal($i){
$l=substr($i,-1);$s=substr($i,-2,-1);return$i.(($l==1&&$s==1)||($l==2&&$s==1)||($l==3&&$s==1)||$l>3||$l==0?'th':($l==3?'rd':($l==2?'nd':'st')));
}
echo 'Today is the '.ordinal(date('z')).' day of this '.ordinal(date('Y')).' year of the common era.';
?>
@joshhartman
joshhartman / gist:836352
Created February 20, 2011 21:59
Automatically Link URLs and Email Addresses Within Text
<?php
function linkify($text) {
$text = preg_replace('/\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[A-Z0-9+&@#\/%=~_|]/i', '<a href="\0">\0</a>', $text);
$text = preg_replace('/\b((mailto:)?[A-Z0-9._%+-]+@[A-Z0-9._%-]+\.[A-Z]{2,4})/i', '<a href="mailto:\0">\0</a>', $text);
return $text;
}
$test_text = "An example of an email address is someone@example.com. Google (http://www.google.com) is the leading search engine! Check out the latest tut @ http://nettuts.com.";
echo linkify($test_text);
?>
@joshhartman
joshhartman / gist:836382
Created February 20, 2011 22:30
Shorten Text Without Breaking Mid-Word
<?php
function shorten($n, $l=75, $e=' ...') {
if ($l >= strlen($n)) { return $n; }
$n = explode(' ', substr($n, 0, $l));
if (count($n)>1) { unset($n[count($n)-1]); }
return implode(' ', $n).$e;
}
echo shorten('Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras gravida purus gravida enim interdum bibendum non vel enim. In et massa eu nibh pellentesque porta.');
?>
@joshhartman
joshhartman / gist:836400
Created February 20, 2011 22:51
Convert a Number of Seconds to a Text String of Days, Hours, Minutes, and Seconds
<?php
date_default_timezone_set('America/Chicago');
function secsToStr($secs) {
if($secs>=86400){$days=floor($secs/86400);$secs=$secs%86400;$r=$days.' day';if($days<>1){$r.='s';}if($secs>0){$r.=', ';}}
if($secs>=3600){$hours=floor($secs/3600);$secs=$secs%3600;$r.=$hours.' hour';if($hours<>1){$r.='s';}if($secs>0){$r.=', ';}}
if($secs>=60){$minutes=floor($secs/60);$secs=$secs%60;$r.=$minutes.' minute';if($minutes<>1){$r.='s';}if($secs>0){$r.=', ';}}
$r.=$secs.' second';if($secs<>1){$r.='s';}
return $r;
}
echo secsToStr(time()-strtotime('1/1/2011'));
@joshhartman
joshhartman / gist:836401
Created February 20, 2011 22:54
Parse XML with SimpleXML
<?php
//this is a sample xml string
$xml_string="<?xml version='1.0'?>
<moleculedb>
<molecule name='Benzine'>
<symbol>ben</symbol>
<code>A</code>
</molecule>
<molecule name='Water'>
<symbol>h2o</symbol>
@joshhartman
joshhartman / gist:843873
Created February 25, 2011 14:42
Get Recent Tweets by Username from Twitter API
<?php
class TwitterFeed {
private var $feed_url;
private var $feed_data;
public function __construct($username, $limit=5){
date_default_timezone_set('America/Chicago');
$this->feed_url = "http://search.twitter.com/search.atom?q=from:".$username."&rpp=".$limit;
$this->feed_data = simplexml_load_file($this->feed_url);
}
@joshhartman
joshhartman / gist:845851
Created February 27, 2011 02:30
WordPress 3.1 Job Opportunities Custom Post Type & Taxonomy Setup
<?php
register_post_type('job-opportunities', array(
'labels' => array(
'name' => 'Job Opportunities',
'singular_name' => 'Job Opportunity',
'add_new' => 'Add New Job',
'add_new_item' => 'Add New Job Opportunity',
'edit_item' => 'Edit Job',
'new_item' => 'New Job Opportunity',