Skip to content

Instantly share code, notes, and snippets.

@tollmanz
tollmanz / wordpress-exporter-unlimited-execution-time.php
Created November 13, 2011 19:58
Increase WordPress Exporter Script Execution Time
// Run function only when the export is initiated
add_action( 'export_wp', 'zdt_unlimited_script_time' );
/**
* Sets script execution time to unlimited.
*
* @return void
*/
function zdt_unlimited_script_time() {
set_time_limit( 0 );
@tollmanz
tollmanz / gist:1968124
Created March 3, 2012 20:46
Get Expired Transient
/**
* Used in front end requests to get the latest Tweet.
*
* @return bool|string
*/
function get_latest_tweet() {
// Attempt to get the transient
if ( $tweet = get_transient( 'my_latest_tweet' ) ) {
return $tweet;
@tollmanz
tollmanz / front-end-display.php
Created March 6, 2012 04:46
Pinterest image functions
<?php if ( $image = zdt_get_social_image( $post ) ) : ?>
<div class="pin_share">
<a href="http://pinterest.com/pin/create/button/?url=<?php echo urlencode( esc_url( get_the_permalink( get_the_ID() ) ); ?>&media=<?php echo urlencode( esc_url( $image ) ); ?>&description=<?php echo urlencode( esc_html( get_the_excerpt( get_the_ID() ) ); ?>" class="pin-it-button" count-layout="none">Pin It</a>
</div>
<?php endif; ?>
@tollmanz
tollmanz / gist:1986992
Created March 6, 2012 15:52
Happy AJAX on .com
/**
* Generates a domain-mapping safe URL on WordPress.com
* Core's ajaxurl uses admin_url() which returns *.wordpress.com which doesn't work for the front-end on domain-mapped sites.
* This works around that and generates the correct URL based on context.
*/
function admin_ajax_url( $path = '' ) {
if ( is_admin() )
$url = admin_url( 'admin-ajax.php' );
else
$url = home_url( 'wp-admin/admin-ajax.php' );
@tollmanz
tollmanz / memcached-vals.php
Created March 12, 2012 18:13
Return values for memcache in WordPress
<?php
add_action( 'init', 'test' );
function test() {
// Returns bool( false )
set_transient( 'test-trans-false', false, 5 );
get_transient( 'test-trans-false' );
// Return bool( false )
set_transient( 'test-trans-true', true, 5 );
get_transient( 'test-trans-true' );
@tollmanz
tollmanz / gist:2209983
Created March 26, 2012 21:43
Add enctype to post form
<?php
function zt_add_enctype {
echo ' enctype="multipart/form-data"';
}
add_action( 'post_edit_form_tag', 'zt_add_enctype' );
@tollmanz
tollmanz / you-lock-it-up.php
Created April 10, 2012 05:26
A locking technique for high concurrency situations in WordPress
<?php
function test_race() {
if ( wp_cache_get( 'lock' ) )
return false;
wp_cache_add( 'lock', 1 );
$key = time();
wp_cache_add( $key, 0 );
@tollmanz
tollmanz / no-you-lock-it-up.php
Created April 10, 2012 15:50
Another locking technique that should only allow one execution per hour
<?php
function test_race() {
$key = date( 'YdmH', time() );
wp_cache_add( $key, 0 );
if ( 1 !== wp_cache_incr( $key ) )
return false;
// Do the things
}
@tollmanz
tollmanz / global-post-snafu.php
Created April 10, 2012 19:13
Fix for messing up the global $post object on a write screen
<?php
function meta_box_callback( $post_local ) {
global $post;
$post_global_copy = $post;
$my_query = new WP_Query( array(
'posts_per_page' => 10
) );
if ( $my_query->have_posts() ) {
<?php
class MostDiscussed_Widget extends WP_Widget {
private $excluded_posts;
function __construct() {
}
function widget( $args, $instance ) {