Skip to content

Instantly share code, notes, and snippets.

@rileypaulsen
rileypaulsen / GoogleSpreadsheets.json.php
Last active December 27, 2015 19:18
Outputs a Google Spreadsheet's CSV feed as valid JSON
<?php
header('Content-type: application/json');
// Based on http://www.ravelrumba.com/blog/json-google-spreadsheets/
//#########################################################
$key = '';
$gid = '0'; //the sheet of the spreadsheet
//#########################################################
@rileypaulsen
rileypaulsen / Select terms from Taxonomy
Last active December 22, 2015 11:29
SQL select all terms from a taxonomy
SELECT a.name as term_name, wp_term_taxonomy.term_id
FROM wp_term_taxonomy
JOIN
wp_terms a
ON (wp_term_taxonomy.term_id = a.term_id)
LEFT JOIN
wp_terms b
ON (wp_term_taxonomy.parent = b.term_id)
WHERE wp_term_taxonomy.taxonomy = 'newscategory'
GROUP BY wp_term_taxonomy.term_id
@rileypaulsen
rileypaulsen / meta_key => post thumbnail
Created August 22, 2013 15:55
query posts argument for getting posts with a post thumbnail set
'meta_key' => '_thumbnail_id'
@rileypaulsen
rileypaulsen / query events by postmeta date field
Created August 15, 2013 05:39
list upcoming events in a CPT based on a postmeta value for date (meta query)
$args = array(
'post_type'=>'event',
'numberposts'=>-1,
'post_status'=>'publish',
'no_found_rows'=>true,
'meta_query'=>array(
array(
'key'=>'date',
'value'=>date('Y-m-d'),
'compare'=>'>='
@rileypaulsen
rileypaulsen / Facebook Counts
Created August 15, 2013 02:30
get the number of Facebook likes, shares, comments, etc. for a given URI
function get_facebook_likes(){
//based on http://halgatewood.com/get-number-of-facebook-likes-for-a-url/
$query = "select total_count,like_count,comment_count,share_count,click_count from link_stat where url='{$uri}'";
$call = "https://api.facebook.com/method/fql.query?query=" . rawurlencode($query) . "&format=json";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $call);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
@rileypaulsen
rileypaulsen / json directory listing
Created August 12, 2013 04:45
list the files in a directory in JSON
<?php
$files = array();
if ($handle = opendir('.')):
while (false !== ($entry = readdir($handle))):
if ( !empty($entry) && $entry != "." && $entry != ".." && $entry != "" && $entry != 'index.php'):
$files[] = $entry;
endif;
endwhile;
closedir($handle);
endif;
@rileypaulsen
rileypaulsen / directory listing
Created August 12, 2013 04:42
list the files in a directory
<?php
echo '<ul>';
if ($handle = opendir('.')) {
while (false !== ($entry = readdir($handle))) {
if ( !empty($entry) && $entry != "." && $entry != ".." && $entry != "" && $entry != 'index.php') {
echo '<li><a href="'.$entry.'">'.$entry.'</a></li>'.PHP_EOL;
}
}
closedir($handle);
}
@rileypaulsen
rileypaulsen / console polyfill
Created August 8, 2013 14:21
define a global console object for browsers that lack one
//stupid IE & mobile safari
if (typeof(console) === 'undefined') {
console = {
log : function(data){},
table : function(data){}
};
}
@rileypaulsen
rileypaulsen / breadcrumbs.php
Created August 3, 2013 13:51
display a breadcrumb trail of page ancestors, with alternative cases for custom post types
<?php
$type = get_post_type();
$ancestors = array_reverse(get_post_ancestors($post->ID));
if( $type == 'page' ) : ?>
<nav id="breadcrumbs">
<ul>
<?php foreach($ancestors as $ancestor): ?>
<li><a href="<?php echo get_permalink($ancestor); ?>"><?php echo get_the_title($ancestor); ?></a></li>
@rileypaulsen
rileypaulsen / MySQL -> JSON by Column
Created July 31, 2013 20:32
generate JSON with rows grouped by a specific column
<?php
mysql_connect("localhost", "root", "");
mysql_select_db("coins");
$result = mysql_query("select * from coins");
$list = array();
while ($r = mysql_fetch_object($result)) {
$list[$r->society][] = $r->ID;
}
print_r(json_encode($list));