Skip to content

Instantly share code, notes, and snippets.

View RadGH's full-sized avatar

Radley Sustaire RadGH

View GitHub Profile
@RadGH
RadGH / gist:5556174
Last active December 17, 2015 05:09
MySQL/Wordpress: Select number of posts by month/year/day/etc, optionally filtered by category
-- Select the count of posts grouped by year, month, day, hour, even minutes and seconds!
SELECT
-- Number of posts on the specified time range
COUNT(*) as "count",
-- Time values to return
-- These should also be listed under "group by"
-- If sorting is necessary, also include these in "order by"
YEAR(p.post_date) as "year",
MONTH(p.post_date) as "month",
@RadGH
RadGH / gist:5556804
Last active December 17, 2015 05:19
Convert parts of a string to date components, noted by a percent sign (%)
<?php
/* ----------------------------
Parse a string with percent-signed wildcards, where the wildcards are PHP date characters.
Example:
$string = "Today is %F %j%S"
Returns: Today is January 1st
*/
function parse_date($string, $date = false) {
if ( !$date ) $date = time();
@RadGH
RadGH / gist:5569528
Created May 13, 2013 16:16
Easy to use PHP mail function for smtp using phpmailer
<?php
/*
--- Send mail using SMTP
Parameters:
to (string - required, email address of recipient)
to_name (string -optional, name of recipient
from (string - required, email address of sender)
from_name (string - optional, name of sender)
subject (string - optional, inherits 40 char from body if blank)
@RadGH
RadGH / gist:5952163
Last active December 19, 2015 11:59
Change the default "From" address for Wordpress.
<?php
function change_wp_mail_sender( $phpmailer ) {
$from_name = 'My Website';
$from_email = get_option('admin_email'); // The same email seen in Settings > General
if (
// We should always have these parameters, but it doesn't hurt to avoid errors in the future
property_exists( $phpmailer, 'From' )
&& property_exists( $phpmailer, 'FromName' )
@RadGH
RadGH / upload-and-process.php
Created July 17, 2013 22:29
Upload and process an image in Wordpress
<?php
$image = abjj_upload_image('profile_picture', $existing_image, 'abjj_process_profile_picture' );
echo '<pre style="font-size: 11px;">RESULT: ---------------------------', "\n",
htmlspecialchars( print_r( $image, true)),
'</pre>';
exit;
// Other functions
// Either process and upload a new image, or retrieve the old image
// $process is a callback that allows the image to be processed once uploaded via Wordpress' image editor
@RadGH
RadGH / wpsc-get-owned-products.php
Last active December 20, 2015 03:49
These two functions allow you two retrieve the products purchased by a user. One returns an array of all objects with crucial purchase log/cart info, the other returns true or false whether the specific product has been purchased.
<?php
function wpsc_get_owned_products( $user_id = false ) {
global $user_purchases, $wpdb;
// Get current user by default
if ( !$user_id ) {
$current = wp_get_current_user();
if ($current) $user_id = $current->ID;
else return false;
@RadGH
RadGH / autofill-dummy-form.js
Last active April 14, 2023 16:31
This javascript will fill all form fields with "dummy" (testing) information. Email fields will be appended @example.org. Phone number fields will generate a 10-digit number. Confirmation fields will match their original fields (assuming they have "confirm" in the name, eg "confirm-email"). Buttons and hidden inputs will be ignored. Click and ch…
function randomstr( length, digits ) {
if ( typeof length == 'undefined' ) length = 6;
if ( typeof digits == 'undefined' ) digits = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
var str = '';
for( i = 0; i < length; i++ ) str += digits.charAt( Math.floor( Math.random() * digits.length ) );
return str;
}
@RadGH
RadGH / add-user-caps.php
Created August 14, 2013 17:16
This script adds capabilities to wordpress role by role name
<?php
/* --------------------------------------------------
/ If used within a plugin, you will want to run this function on plugin activation
/ For development, you can simply call the function below . To do this, uncomment
/ the line below, then visit any page.
-------------------------------------------------- */
// custom_user_roles();
function custom_user_roles() {
@RadGH
RadGH / rate-limiter.js
Last active December 21, 2015 09:28
This is a simple JavaScript rate limiter that you can stick in any function
// This script prevents a function from running too frequently.
// Pick one of these three variations, then copy the code to a function of your choice. Do not copy the entire script.
// VARIATION A: Simply ignore any excessive function calls
// VARIATION B: Ignore excessive function calls, but call the function again once the limiter has expired. Ignore parameters.
// VARIATION C: Like variation B, call the function after limiter expired - but pass in function's parameters.
// Variation A ----------------
// Replace 150 with the rate limit amount of your choice.
@RadGH
RadGH / easy-lightbox.js
Last active December 23, 2015 00:49
Easy lightbox
/*
Accompanying CSS is commented below.
Example Usage:
show_lightbox("Hello World", "Good morning, friend", {ok_button: true});
---
show_lightbox( title, content, [options] )
Title: The title of the lightbox. Optional. If only title is specified, the title will be used as content instead.