Skip to content

Instantly share code, notes, and snippets.

@tollmanz
tollmanz / gist:2641305
Created May 9, 2012 02:25
wp_list_pluck Example
<?php
// Take from posts-to-posts (http://scribu.net/wordpress/posts-to-posts)
public function get_related( $post_id, $extra_qv = array() ) {
$post_id = (array) $post_id;
$connected = $this->get_connected( $post_id, $extra_qv );
if ( !$connected )
return false;
if ( !$connected->have_posts() )
@tollmanz
tollmanz / gist:2708692
Created May 16, 2012 08:27
A Basic Caching "Get" Function
<?php
function get_hottest_cities( $force = false ) {
$hottest_cities = get_transient( 'zdt-hottest-cities' );
if ( false === $hottest_cities || $force ) {
$hottest_cities = retrieve_hottest_cities();
if ( $hottest_cities )
set_transient( 'zdt-hottest-cities', $hottest_cities );
}
return $hottest_cities;
@tollmanz
tollmanz / gist:2708698
Created May 16, 2012 08:28
Caching with a "Backup"
<?php
function get_hottest_cities( $force = false ) {
$hottest_cities = get_transient( 'zdt-hottest-cities' );
if ( false === $hottest_cities || $force ) {
if ( $force ) {
$hottest_cities = retrieve_hottest_cities();
if ( $hottest_cities ) {
set_transient( 'zdt-hottest-cities', $hottest_cities );
@tollmanz
tollmanz / gist:2760476
Created May 21, 2012 03:47
Get a Unique Key for Current Object
<?php
/**
* Get a unique ID key for current $wp_the_query object.
*
* @return string|int unique key on success, 0 on failure
*/
function zdt_get_object_key() {
$queried_object = get_queried_object();
$queried_object_id = get_queried_object_id();
@tollmanz
tollmanz / gist:2821651
Created May 28, 2012 23:32
An Example Cache Refresh Function
<?php
function ohsc_get_ratings( $prime_cache = false ) {
$movies = array(
'The Usual Suspects',
'Braveheart',
'The King\s Speech'
);
$cache_key = 'ohsc-' . serialize( $movies );
@tollmanz
tollmanz / gist:2821654
Created May 28, 2012 23:32
Adding a Refresh Cache Menu Item
<?php
function ohsc_add_get_ratings_refresh() {
$button = array(
'id' => 'ohsc-add-get-ratings-refresh',
'title' => 'Movie Ratings',
'function' => 'ohsc_get_ratings',
'args' => array( true ),
);
afc_add_item( $button );
}
@tollmanz
tollmanz / gist.css
Created June 3, 2012 19:16
Add Cacheable Gists to Your WordPress Site
.gist {
color: #e4322e;
}
.gistdiv {
padding: 0;
margin: 0;
}
.gist .gist-file {
@tollmanz
tollmanz / gist:3882185
Created October 12, 2012 23:16
Basic Google Analytics Caching
<?php
function zt_get_top_posts() {
// Define the cache key
$cache_key = 'zt-top-posts';
// Attempt to get data from cache
$top_posts = wp_cache_get( $cache_key );
// If not found in cache regenerate
if ( false === $top_posts ) {
@tollmanz
tollmanz / gist:3882192
Created October 12, 2012 23:17
Get Google Analytics Data
<?php
function zt_generate_top_posts() {
// These URLs will not be be returned
$blacklisted_paths = array(
'/',
'about-us',
'that-page-no-one-needed-to-see'
);
// URLs with these components will not be returned
@tollmanz
tollmanz / gist:3882195
Created October 12, 2012 23:18
Cache Google Analytics Data with Force Argument
<?php
function zt_get_top_posts( $force = false ) {
// Define our cache key
$cache_key = 'zt-top-posts';
// Attempt to grab data from cache
$top_posts = wp_cache_get( $cache_key );
// If not found in cache or forced, regenerate
if ( false === $top_posts || true === $force ) {