Skip to content

Instantly share code, notes, and snippets.

@benmay
benmay / reset_user_roles.sql
Last active August 29, 2015 13:56
resets WP default user roles
UPDATE `wp_options` SET `option_value` = 'a:7:{s:13:"administrator";a:2:{s:4:"name";s:13:"Administrator";s:12:"capabilities";a:63:{s:13:"switch_themes";b:1;s:11:"edit_themes";b:1;s:16:"activate_plugins";b:1;s:12:"edit_plugins";b:1;s:10:"edit_users";b:1;s:10:"edit_files";b:1;s:14:"manage_options";b:1;s:17:"moderate_comments";b:1;s:17:"manage_categories";b:1;s:12:"manage_links";b:1;s:12:"upload_files";b:1;s:6:"import";b:1;s:15:"unfiltered_html";b:1;s:10:"edit_posts";b:1;s:17:"edit_others_posts";b:1;s:20:"edit_published_posts";b:1;s:13:"publish_posts";b:1;s:10:"edit_pages";b:1;s:4:"read";b:1;s:8:"level_10";b:1;s:7:"level_9";b:1;s:7:"level_8";b:1;s:7:"level_7";b:1;s:7:"level_6";b:1;s:7:"level_5";b:1;s:7:"level_4";b:1;s:7:"level_3";b:1;s:7:"level_2";b:1;s:7:"level_1";b:1;s:7:"level_0";b:1;s:17:"edit_others_pages";b:1;s:20:"edit_published_pages";b:1;s:13:"publish_pages";b:1;s:12:"delete_pages";b:1;s:19:"delete_others_pages";b:1;s:22:"delete_published_pages";b:1;s:12:"delete_posts";b:1;s:19:"delete_others_posts";b:1
@benmay
benmay / gist:222db7df6aecedb2c5e7
Created June 4, 2014 23:55
Get files from prod if they don't exist on local dev / staging. Put this snippet BEFORE the WordPress rule in htaccess.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule wp-content/uploads/(.*) http://PRODUCTION_SERVER/wp-content/uploads/$1 [NC,L]
</IfModule>
@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 / 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 / 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 / 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: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>";