Skip to content

Instantly share code, notes, and snippets.

@clarklab
clarklab / wordpress-find-discount.php
Created January 27, 2012 02:22
PHP/WordPress find discount
<?php
$deal_price = get_post_meta($post->ID, 'deal_price', true);
$old_price = get_post_meta($post->ID, 'old_price', true);
$discount = round(((1-($deal_price / $old_price))*100),0);
echo $discount.'%'; ?>
@clarklab
clarklab / WP-Transient-API.php
Created January 27, 2012 21:49
Using get_transient and set_transient to save changing data
<?php
//let's get my klout score!!
//first, let's see if I've got the data already cached locally
$klout = get_transient("klout");
//dang it! it looks like I might not. let's check with the Klout API
if( !$klout ) {
$json = file_get_contents("http://api.klout.com/1/klout.json?key=[API key]&users=clarklab");
@clarklab
clarklab / wp_insert_post_front_end_form.php
Created May 25, 2012 00:12
This is a sample front-end form using wp_insert_post(). It's quickly stripped out of my production code from Android and Me, and hasn't been tested alone, so please take it with a grain of salt.
<?
if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['insert_post'] )) { //check that our form was submitted
$title = $_POST['thread_title']; //set our title
if ($_POST['thread_description']=="") { // check if a description was entered
$description = "See thread title..."; // if not, use placeholder
} else {
$description = $_POST['thread_description']; //if so, use it
@clarklab
clarklab / simple_point_system.php
Created July 5, 2012 22:48
A simple point system in WordPress. I like this function almost *too much*. Please use it wisely.
<?php
function update_user_points($id, $value){
$points = get_user_meta($id, 'points', true);
$points = $points + $value;
update_user_meta($id, 'points', $points);
return $points;
}
@clarklab
clarklab / page-redirect.php
Created July 6, 2012 03:12
Simple redirect (URL shortener / tracker) page template
<?php
/*
Template Name: Redirect
*/
$redirect_url = get_post_meta($post->ID, 'redirect_url', true);
// get the destination URL
$count = get_post_meta($post->ID, 'count', true);
// get the number of times it's been clicked
@clarklab
clarklab / exclude_previously_looped_posts.php
Created July 18, 2012 02:24
A simple way of making sure not to display the same posts twice when using multiple loops. Save the post ID's into an array, then exclude those posts from future loops.
<?php
//start a new WP_Query with whatever parameters you want
$firstquery = new WP_Query(array(
'posts_per_page' => 2
'tag' => 'summer'
));
//start some looping action on $firstquery
while ($firstquery->have_posts()) : $firstquery->the_post();
@clarklab
clarklab / gist:3702300
Created September 11, 2012 21:42
WP_Query snippet for Sublime Text 2
<snippet>
<content><![CDATA[
\$${1:query} = new WP_Query('posts_per_page=5');
while (\$${1:query}->have_posts()) : \$${1:query}->the_post();
${0:// do something}
endwhile;
]]></content>
<tabTrigger>wpq</tabTrigger>
<scope>source.php</scope>
<description>WP_Query basic loop</description>
@clarklab
clarklab / gist:3902885
Created October 17, 2012 00:01
get_theme_mod
<!-- in footer.php -->
<div class="footer-text"><?php echo get_theme_mod('footer_text') ?></div>
<!-- rendered HTML content -->
<div class="footer-text">&copy;2012 Site Name</div>
<!--in functions.php (to enable the field in the Theme Customizer) -->
<?php
@clarklab
clarklab / gist:3932502
Created October 22, 2012 16:49
Post format picker
function post_format_picker () {
$screen = get_current_screen();
if ( $screen->id == 'post' ) {
echo '<script>jQuery(document).ready(function(){
var hash = window.location.hash;
if (hash){
jQuery("a[href$="+hash+"]").click();
@clarklab
clarklab / wp_query.sublime-snippet
Created October 29, 2012 20:49
WP_Query snippet for Sublime Text 2
<snippet>
<content><![CDATA[
\$${1:query} = new WP_Query('${2:posts_per_page=5}');
while (\$${1:query}->have_posts()) : \$${1:query}->the_post();
${3:// do something}
endwhile;
]]></content>
<tabTrigger>wpq</tabTrigger>
<scope>source.php</scope>
<description>WP_Query basic loop</description>