Skip to content

Instantly share code, notes, and snippets.

View aaroncampbell's full-sized avatar

Aaron D. Campbell aaroncampbell

View GitHub Profile
<?php
function test_multiple_shortcode_unautop() {
$test_strings = array(
"[footag][footag]\n",
"[footag]\n[footag]\n",
"[footag]\n\n[footag]\n",
);
foreach( $test_strings as $test_string ) {
$actual = shortcode_unautop( wpautop( $test_string ) );
@aaroncampbell
aaroncampbell / ithemes-exchange-inventory.php
Created March 27, 2015 03:45
Show different strings for stock level of 0, 1, and many items
<?php
$inventory = it_exchange( 'product', 'inventory', 'return=true' );
if ( 0 == $inventory ) {
echo 'Too Late!'; // We're completely out
} elseif ( 1 == $inventory ) {
echo 'There can be only one!'; // There is one in stock
} else {
printf( 'We have %s in stock.', number_format( $inventory, 0, null, ',' ) );
}
@aaroncampbell
aaroncampbell / README.md
Created September 5, 2011 19:38 — forked from markjaquith/README.md
TLC Transients — On the fly WordPress transients with soft expiration and one-liner chained syntax.

TLC Transients

A WordPress transients interface with support for soft-expiration (use old content until new content is available), background updating of the transients (without having to wait for a cron job), and a chainable syntax that allows for one liners.

Examples

In this simple example, we're defining a feed-fetching callback, and then using tlc_transient with a chain to point to that callback and use it, all in one line. Note that since we haven't used background_only(), the initial load of this will cause the page to pause.

@aaroncampbell
aaroncampbell / expand-admin-menus.php
Created October 12, 2011 15:23
WordPress Expanded Admin Menus
<?php
/**
* Plugin Name:
* Plugin URI: http://xavisys.com/wordpress-plugins/expanded-admin-menus/
* Description: Forces all admin menus to be expanded all the time
* Version: 0.0.1
* Author: Aaron D. Campbell
* Author URI: http://xavisys.com/
*/
<?php
/**
* If the post doesn't have a featured image set it to the first attached image
* or a default if there are no images
*/
function pd_set_post_thumbnail( $post_id = null ) {
if ( ! isset( $post_id ) )
$post_id = get_the_ID();
if ( has_post_thumbnail( $post_id ) )
return true;
@aaroncampbell
aaroncampbell / gist:4483989
Last active December 10, 2015 19:48
Dump function for convenient debugging. It can handle anything var_dump() can, but also takes a title that it will insert above the data, as well as formatting the data for display in HTML, PLAINTEXT, or as an HTML comment.
<?php
/**
* For use with debugging
*/
if ( !function_exists('dump') ) {
/**
* To be used like a more flexible more developer friendly var_dump.
* Basically you can pass a variable to dump as well as a title and choose
* whether it returns that data to you to echo out or Use this as a var_dump
*
@aaroncampbell
aaroncampbell / redgreen.sh
Last active December 11, 2015 21:58
Measure the added and deleted lines in patches
# For patches, lower numbers are better
function redgreen() {
sed s/'^\(---\|+++\)'// | ack "^[-|\+]" -oh | sort | uniq -c | sed s/"\s*\([0-9]\+\)\s*\([+-]\)"/"\2\1"/ | xargs | awk '{print $2}{print $1}{print "--\r\n"$2 + $1}'
}
# Example usage
# $ svn di | redgreen
# +205
# -506
# --
@aaroncampbell
aaroncampbell / shopp-cart-sort.php
Last active December 15, 2015 03:59
Sorting Shopp cart by blog_id (product meta data)
<?php
foreach ( ShoppOrder()->Cart->contents as &$item ) {
$item->blog_id = get_post_meta( $item->product, 'blog_id', true );
}
function finecabin_sort_cart( $a, $b ) {
if ( $a->blog_id == $b->blog_id )
return 0;
return ( $a->blog_id < $b->blog_id )? -1 : 1;
}
@aaroncampbell
aaroncampbell / get_php_bin_from_path.php
Created August 21, 2013 13:34
Find the PHP binary in the path
<?php
/**
* Checks the path for the php binary and returns it's location if found
*
* @return false|string False on failure, String of binary location on success
*/
function get_php_bin_from_path() {
$paths = explode( PATH_SEPARATOR, getenv( 'PATH' ) );
foreach ( $paths as $path ) {
// Windows XAMPP sometimes has the actual bin as the path
@aaroncampbell
aaroncampbell / show-related-thumbs.php
Created September 18, 2013 15:05
Showing related post thumbnails using Efficient Related Posts for WordPress