Skip to content

Instantly share code, notes, and snippets.

@benmay
benmay / single-user-loggin.php
Last active March 14, 2019 17:51
Ensures only one user at a time can be logged into WordPress, ie, 2 people can't login using the same account.
<?php
/*
Plugin name: Single user login
Plugin URI:
Description:
Author: Ben May
Author URI:
Version: 0.1
*/
<?php
add_action ( 'admin_init', 'clean_up_admin_menu' );
function clean_up_admin_menu ()
{
global $current_user, $menu;
$u = $current_user->user_login;
// If user isn't logged in. (fix for wp-admin/admin-ajax.php
@benmay
benmay / gravity_merge_tags.php
Last active December 14, 2015 11:38
Add WP_login merge tag to gravity forms
<?php
class extraGravityMergeTags
{
function __construct()
{
add_filter( 'gform_custom_merge_tags', array( $this, 'add_merge_tags' ), 10, 4 );
add_filter( 'gform_replace_merge_tags', array( $this, 'run_merge_tags' ) , 10, 7);
}
@benmay
benmay / num_comments.sql
Created February 21, 2013 06:36
Grab number of comments on posts during 2012, in category cricket.
SELECT SUM(p.comment_count) FROM wp_posts p
JOIN wp_term_relationships tr ON tr.object_id = p.ID
JOIN wp_term_taxonomy tt ON tt.term_taxonomy_id = tr.term_taxonomy_id
JOIN wp_terms t ON t.term_id = tt.term_id
WHERE t.name = 'Cricket'
AND `post_date` >= '2012-01-01 00:00:00'
AND `post_date` <= '2013-01-01 00:00:00'
AND p.post_status = 'publish'
@benmay
benmay / sub-repeater-acf-migration.php
Created February 17, 2013 04:48
Had to split a repeatable section from an ACF post, to it's own post, and then use Posts 2 Posts plugin to preserve the relationship. This managed to keep all the meta data intact for ACF to pick it up (as long as the fields in the new group had the same name) Top level repeater was 'brands' under a post in the advertisers cpt.
$args = array(
'post_status' => array( 'pending', 'draft', 'publish' ),
'posts_per_page' => -1,
'post_type' => 'advertisers'
);
$query = new WP_Query( $args );
while( $query->have_posts() ) {
@benmay
benmay / simple-download-tracker.php
Created December 11, 2012 10:02
simple downloader tracker / counter
<?php
/*
Plugin Name: Download counter
Plugin URI:
Description: Very simple download counter
Author: Ben May
Version: 0.1
Author URI:
*/
@benmay
benmay / custom_post_type_taxonomy_date_archives.php
Created November 4, 2012 01:39
Filter Custom Post Type, Taxonomy by date archives
/*
Problem was, a custom post type, with custom taxonomy, and then wanted to have a dropdown of archives by month/year. wp_get_archives() wouldn't work clearly.
Rewrite rule required to be added:
$new_rule = 'media-centre/format/media-releases/archives/(.+)/(.+)/?$' => 'index.php?post_type=mc&mc-format=media-releases&year=' . $wp_rewrite->preg_index(1) .'&monthnum=' . $wp_rewrite->preg_index(2);
*/
@benmay
benmay / resort-posts-array.php
Created October 29, 2012 04:25
On taxonomy page. Wanted to sort posts by their membership to a taxonomy. Loops through and re-orders as per the order of $terms array.
/*
* Used on taxonomy.php template by calling $posts = resort_posts($posts);
* =============================
* Expects the $posts variable to be passed to it, and will loop through,
* and move all the image gallery posts to the end of the loop.
*
* Order is by following taxonomies:
* - Media Releases
* - Fact Sheets
* - Images
@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;
}
@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>";