Skip to content

Instantly share code, notes, and snippets.

View gregrickaby's full-sized avatar
:octocat:

Greg Rickaby gregrickaby

:octocat:
View GitHub Profile
@gregrickaby
gregrickaby / wordpress-transient-caching.php
Last active January 2, 2019 10:45
WordPress Transient Caching
<?php
/**
* Setup wp_query arguments for the loop. Cache the results for 4 hours.
*
* @link http://codex.wordpress.org/Transients_API
*/
// Check for transient
if ( false === ( $my_query = get_transient( 'foo_featured_posts' ) ) ) {
<?php
/**
* WordPress Query Comprehensive Reference
* Compiled by luetkemj - luetkemj.com
*
* CODEX: http://codex.wordpress.org/Class_Reference/WP_Query
* Source: http://core.trac.wordpress.org/browser/tags/3.3.1/wp-includes/query.php
*/
$args = array(
@gregrickaby
gregrickaby / functions.php
Last active September 26, 2022 04:27
Show me dev info in WordPress
add_action( 'wp_footer', 'wds_debug' );
/*
* Show me some dev info during development! DELETE ME BEFORE GOING LIVE!!!
*
*/
function wds_debug() {
global $template; ?><br />
<?php echo $template; ?><br />
<?php echo get_num_queries(); ?> queries in <?php timer_stop(1); ?> seconds<br />
Server load: <?php $load = sys_getloadavg(); echo $load[0]; ?>%<br />
@gregrickaby
gregrickaby / functions.php
Created July 2, 2013 13:45
Get custom options from WordPress database
/**
* Create helper function to easily get options from database
*
* @since Child 1.0
*/
function child_get_option( $key, $setting = null ) {
$setting = $setting ? $setting : 'child_options'; // this must match your custom options in the database.
$options = get_option( $setting );
return is_array( $options[$key] ) ? stripslashes_deep( $options[$key] ) : stripslashes( wp_kses_decode_entities( $options[$key] ) );
}
@gregrickaby
gregrickaby / html5-schema.org-markup.html
Last active August 2, 2022 00:05
Proper SCHEMA.ORG markup
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="robots" content="noodp, noydir" />
<link rel="dns-prefetch" href="//cdnjs.cloudflare.com">
<link rel="canonical" href="http://mysite.com/" />
<link rel="stylesheet" href="http://mysite.com/style.css" type="text/css" />
@gregrickaby
gregrickaby / even_odd_classes.php
Last active November 20, 2022 12:45
Even odd classes in WordPress loop
<?php
// WP_Query Arguments
$args = array(
'order' => 'DESC',
'posts_per_page' => 5
);
// The Loop
$query = new WP_Query( $args );
@gregrickaby
gregrickaby / page-all.php
Last active November 26, 2018 01:39
Display all posts in a page template
<?php
/**
* Template Name: All Posts
*
* This template lists all posts via WP_Query and get_posts();
*/
get_header();
// WP_Query arguments
$args = array (
@gregrickaby
gregrickaby / numbered_post_class_loop.php
Last active November 26, 2018 01:39
Add post count class to WordPress loop
<?php
// Count the posts and give them each a unqiue number as a css class
$count = 1;
while ( $query->have_posts() ) : $query->the_post(); $count <= 100; ?>
<div class="count-<?php echo $count++; ?>">
<p class="headline"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></p>
</div>
<?php endwhile; ?>
@gregrickaby
gregrickaby / display-social-media-counts.php
Last active February 6, 2017 08:56
Display Likes, Tweets, Pageviews, and Comment Counts. Use transient cache to store data for 30 minutes.
<?php
/**
* Get tweet count from Twitter API (v1.1)
*/
function wds_post_tweet_count( $post_id ) {
// Check for transient
if ( ! ( $count = get_transient( 'wds_post_tweet_count' . $post_id ) ) ) {
@gregrickaby
gregrickaby / jquery-ajax-modal.js
Last active January 24, 2017 07:57
Simple jQuery Ajax Modal with fail-safe.
/**
* Loading Modal
*/
$(body).on({
// When ajaxStart is fired, add 'loading' to body class
ajaxStart: function() {
$(this).addClass('modal-loading');