Skip to content

Instantly share code, notes, and snippets.

@benmay
benmay / wp_author_archive_limt
Created May 31, 2012 10:23
Modify num items on author archive
add_filter( 'pre_option_posts_per_page' , 'limit_posts_per_page');
function limit_posts_per_page()
{
global $wp_query;
if ( $wp_query->is_author=='1')
{
return 6;
}
else
@benmay
benmay / custom_pt_modify_loop
Created June 6, 2012 00:42
Modify the query on custom post type archives
add_filter( 'pre_get_posts','add_to_query' );
function add_to_query( $query )
{
if ( is_post_type_archive( 'listings' ) ):
$query -> set( 'orderby' , 'title' );
$query -> set( 'order' , 'asc' );
endif;
return $query;
}
@benmay
benmay / humanise_date_diff
Created June 6, 2012 01:25
Humanise date diff - ie, 3 days ago etc.
function datediff($timestamp)
{
$difference = time() - strtotime($timestamp);
if($difference < 60){
$s = ($difference == '1' ? '' : 's');
return $difference." second$s ago";
}else{
$difference = round($difference / 60);
if($difference < 60){
@benmay
benmay / form_validatin_functions.php
Created June 6, 2012 01:40
Simple handy form validation functions used on a few diff forms.
On Form Page:
$form_errors = process_form();
In the form (contains any validation errors)
<?php validation_errors(); ?>
Form Elements (example)
@benmay
benmay / mobile_theme_switcher
Created July 24, 2012 09:50
WordPress plugin - switching themes (used for mobile theme)
<?php
/*
Plugin Name: Mobile Theme Switcher
Description:
Version: 0.1
Author: Ben May
Author URI:
*/
class roarMobile {
@benmay
benmay / gist:3727070
Created September 15, 2012 09:14
Never Moderate WordPress user if custom user meta is set
// if the user_meta 'vsc_fame' is set for a user, then their comments will ALWAYS be approved,
// regardless if they are failed because they contain links etc.
function comments_edr_hofs( $approved , $commentdata )
{
if( isset( $commentdata['user_ID'] ) )
{
if( get_the_author_meta( 'vsc_fame', $commentdata['user_ID'] ) )
{
return 1;
@benmay
benmay / gist:3768884
Created September 23, 2012 04:34
tar website - excluding wp-uploads
tar -zcvf backup-name.tar.gz --exclude='wp-content/uploads/*' /var/www/site.com
@benmay
benmay / gist:3846822
Created October 7, 2012 01:49
Flush WordPress rewrite rules when the MD5 sum of the add post type file changes.
// I put this code in the file where I add my custom post types / taxonomies.
// Flush rewrite rules on change of this file.
if( get_option('custom_pt_rules') != md5_file( __FILE__ ) )
{
add_action( 'init', function()
{
global $wp_rewrite;
$wp_rewrite->flush_rules();
});
update_option('custom_pt_rules', md5_file( __FILE__ ) );
@benmay
benmay / addSelectAlltoTaxGroup.js
Created October 23, 2012 10:04
Adds a "select all" and "deselect" all link to a taxonomy or category box while editing a WordPress post. addSelectAlltoTaxGroup($key) where $key = taxonomy name.
jQuery(document).ready(function($) {
addSelectAlltoTaxGroup( 'region' );
addSelectAlltoTaxGroup( 'program' );
function addSelectAlltoTaxGroup( key )
{
var selectAll = "<span class=\"my-selector\">&nbsp; | <a href=\"#"+key+"-adder\" id=\""+key+"-select-all\">Select All</a></span>";
var deselectAll = "<span class=\"my-selector\">&nbsp; | <a href=\"#"+key+"-adder\" id=\""+key+"-deselect-all\">Deselect All</a></span>";
@benmay
benmay / gist:3949825
Created October 25, 2012 00:39
Allow WP to upload SRT files.
add_filter( 'upload_mimes', array( &$this, 'mimes' ) );
function mimes ( $existing_mimes=array() )
{
$existing_mimes['srt'] = 'text/plain';
return $existing_mimes;
}