Skip to content

Instantly share code, notes, and snippets.

View RadGH's full-sized avatar

Radley Sustaire RadGH

View GitHub Profile
@RadGH
RadGH / woocommmerce-product-variations.php
Created September 25, 2013 18:40
For the WooCommerce plugin for WordPress, this function returns all variations of a product by referencing the product ID.
<?php /*
Takes a product ID and returns an array that includes all types of variations of the product, and the attributes of that variation.
Variations are normally returned as a term object. They belong to the original product ID and the taxonomy name is equal to the attribute name.
If a custom variation is provided instead of a term object, the variation will simply be a string of the option's name.
Example return result is given below. This is a single product with one attribute and two different variations.
Array (
[pa_oregon-training-classes] => Array (
[attribute] => Array (
@RadGH
RadGH / remove-options-framework-sanitization.php
Created October 1, 2013 18:20
Remove sanitization filters from the Options Framework plugin by Devin Price
<?php
// Disable sanitization of specific fields by removing the sanitization filter hooks
// Note that at least one filter must exist for the field to be saved.
function optionsframework_remove_sanitization() {
// Text areas
remove_filter( 'of_sanitize_textarea', 'of_sanitize_textarea' );
add_filter( 'of_sanitize_textarea', function ($input) { return $input; });
@RadGH
RadGH / admin.js
Last active April 7, 2016 08:14
WordPress media "browse" button
/*
HOW TO USE:
0. Ensure the admin page has enqueue the media library scripts. Here is an example to include it on the "Page" edit screen (post.php):
function page_enqueue_media_scripts() {
$screen = get_current_screen();
if ( $screen->base == 'post' && $screen->id == 'page' ) wp_enqueue_media();
}
add_action('admin_enqueue_scripts', 'page_enqueue_media_scripts');
@RadGH
RadGH / 10digitphone.php
Created March 20, 2014 17:17
This function quickly grabs the 10 digit phone number from a string, formatted as (xxx) xxx-xxxx.
<?php
// This function quickly grabs the 10 digit phone number from a string.
// Works with most user input.
// Standard US phone number format only.
// Note: For storage, always store what the user gives you (ex: 555-123-1234 until 5pm, after that call 555-123-1234)
function extract10digitphone( $str, $format = '(%s) %s-%s' ) {
$regex = '/([0-9]{3}).*([0-9]{3}).*([0-9]{4})/mU';
if ( preg_match( $regex, $str, $match ) ) {
@RadGH
RadGH / quick-user-creds.js
Created April 16, 2014 16:39
Quickly create the same user in multiple WordPress sites
// Bookmarklet:
// javascript:(function()%7Bif%20(%20jQuery('%23createuser').length%20)%20%7Bvar%20previous_creds%20%3D%20prompt('Enter%20previous%20setup%20code%20(or%20click%20enter%20to%20skip)'%2C%20'')%3Bvar%20username%20%3D%20false%3Bvar%20email%20%3D%20false%3Bvar%20password%20%3D%20false%3Bvar%20firstname%20%3D%20false%3Bvar%20lastname%20%3D%20false%3Bvar%20website%20%3D%20false%3Bvar%20role%20%3D%20false%3Bif%20(%20previous_creds%20)%20%7Bvar%20creds%20%3D%20jQuery.parseJSON(%20previous_creds%20)%3Bif%20(%20creds%20)%20%7Busername%20%3D%20creds.username%3Bemail%20%3D%20creds.email%3Bpassword%20%3D%20creds.password%3Bif%20(%20creds.firstname%20)%20firstname%20%3D%20creds.firstname%3Bif%20(%20creds.lastname%20)%20lastname%20%3D%20creds.lastname%3Bif%20(%20creds.website%20)%20website%20%3D%20creds.website%3Bif%20(%20creds.role%20)%20role%20%3D%20creds.role%3B%7D%7Dif%20(%20!previous_creds%20%7C%7C%20!username%20%7C%7C%20!email%20%7C%7C%20!password%20)%20%7Bvar%20username%20%3D%20prompt('(Step%201%2F8)%2
@RadGH
RadGH / ftpbackup.sh
Last active August 29, 2015 14:01
Upload a backup file to external FTP. Delete local copy if successful.
#!/bin/bash
# Upload a backup file to external FTP. Delete local copy if successful.
# CONFIGURATION ----------
LOCALPATH='/path/to/backup'
HOST=''
USER=''
PASS=''
REMOTEPATH='/remote/storage/path'
# RUN PROGRAM ------------
/*
This function allows you to press and drag your mouse over multiple checkboxes to change the state of them all.
It does not toggle, but instead causes any selected values to match the originating element.
Does not support radio buttons or dynamically loaded elements.
Example Usage -- Say you have five checkboxes, with the middle one checked:
[0]
[0]
@RadGH
RadGH / weather.php
Created May 12, 2014 18:31
Get the current temperature and weather summary from Forecast.io, cached results
<?php
$recent_weather = get_transient('recent_weather_widget');
if ( !$recent_weather ) {
$api_key = 'YOUR_API_KEY';
$lat = '44.052303';
$long = '-123.102148';
$cache_duration = 60*30; // seconds
// retrieve weather information from forecast.io
@RadGH
RadGH / cmm_quick_field_syntax.php
Last active August 29, 2015 14:01
Quick matadata group and field declaration for the Custom Metadata Manager plugin.
<?php
/*
Format:
array(
group_key => (
post_type = TYPES,
args = ARGS,
fields = FIELDS,
@RadGH
RadGH / wp-media-browse-button.js
Last active August 29, 2015 14:01
Easily create a media browse button, automatically insert URL, title, thumbnail, alt text, etc. into other inputs automatically.
/*
Rad's WordPress Media Upload Button API
Serves as an easy-to-use relay between your form fields and the media browser.
----------
Example Usage:
Retrieve the image URL and the attachment ID of an image with a single "Browse" button, and display a preview of the selected image.