Skip to content

Instantly share code, notes, and snippets.

View JustinSainton's full-sized avatar

Justin Sainton JustinSainton

View GitHub Profile
<?php
/**
* Class: Sample_Block_Parser
*
* @package Sample
* @subpackage sample-project
* @since 1.0.0
*/
add_filter(
@JustinSainton
JustinSainton / import-blocked-attachments.php
Created August 8, 2018 20:53
Import blocked attachments
<?php
add_filter( 'http_request_args', function( $args ) {
if ( ! defined( 'WP_IMPORTING' ) || ( defined( 'WP_IMPORTING' ) && ! WP_IMPORTING ) ) {
return $args;
}
if ( ! isset( $args['headers']['Authorization'] ) ) {
$args['headers']['Authorization'] = 'Basic ' . base64_encode( 'user:pass' );
<?php
function wpsc_get_terms_variation_sort_filter( $terms ) {
$new_terms = array();
$unsorted = array();
var_dump( $terms ); // in 4.5.2, returns an array of terms. In trunk (only in some contexts), returns a numeric string.
foreach ( $terms as $term ) {
if ( ! is_object( $term ) ) {
return $terms;
@JustinSainton
JustinSainton / gist:396c7057a8c4ce19f37c654418ecbfdd
Created May 12, 2016 03:50 — forked from lucasstark/gist:6594983
Example to add meta data to woocommerce cart items
<?php
/*
* Plugin Name: Example Modify Price
*/
class Example_Modify_Price {
private static $instance;
public static function register() {
if (self::$instance == null) {
@JustinSainton
JustinSainton / meta-sticky.php
Last active February 10, 2016 00:51
Ever wanted to use "WHERE meta_key = value" as a type of sticky?
<?php
/**
* Use case: Want to order by meta key = value, while including all posts, not just those matching said meta value.
*
* WP_Query's capacity for ordering has improved so much - but as of right now, it's not possible
* to get _all_ posts, not just ones where a value may or may not exist, and may or may not equal something, and
* have all of those be at the top of results.
*
* My very specific use case: a use can set their city and it's saved as a cookie.
@JustinSainton
JustinSainton / notify.php
Created January 27, 2016 23:03
Use existing Gravity Forms notification on non-form data
<?php
$form_id = 12; // Or whatever form ID has the notificaiton you're after
$form = GFAPI::get_form( $form_id );
$notification = end( $form['notifications'] ); // Just an easy way to get the notification I wanted. YMMV.
$lead = array(
'id' => wp_generate_password( 64, true, true ), // Normally, this would be the Lead ID. Not necessary for us.
'form_id' => $form_id,
'source_url' => 'https://site.com/',
'status' => 'active',
'1.3' => $args['first_name'], // YMMV on these args - set them to whatever your form sets them to be.
@JustinSainton
JustinSainton / use-email-for-login-woocommerce.php
Created January 27, 2016 03:35
Use the submitted email address for the username in WooCommerce registration.
<?php
add_filter( 'woocommerce_new_customer_data', function( $data ) {
$data['user_login'] = $data['user_email'];
return $data;
} );
@JustinSainton
JustinSainton / because-wp-query-will-not-do-this.php
Created January 21, 2016 05:02
Pouring a Drink for Everyone Who Has Had to Order By Term Slug
<?php
add_action( 'pre_get_posts', function( $query ) {
if ( ( is_post_type_archive( 'special-cpt' ) || ( is_search() && 'special-cpt' == $query->get( 'post_type' ) ) ) && $query->is_main_query() ) {
$query->set( 'orderby' , 'title' );
$query->set( 'order' , 'ASC' );
$query->set( 'tax_query', array(
array(
@JustinSainton
JustinSainton / quick-and-dirty.php
Last active October 27, 2015 23:59
Include excerpts in searches
<?php
add_filter( 'posts_search', function( $sql, $wp_query ) {
global $wpdb;
$sql .= "OR $wpdb->posts.post_excerpt LIKE '%s' AND $wpdb->posts.post_type NOT IN ('some-post-type', 'another-post-type')";
return $sql;
}, 10, 2 );
@JustinSainton
JustinSainton / no-ship-sherlock.php
Last active August 29, 2015 14:27
Remove Shipping Fields on a Cart with no Shippable Items
<?php
function wpsc_hide_shipping_fields_if_no_shipping( $fields ) {
if ( wpsc_uses_shipping() || is_admin() ) {
return $fields;
}
foreach ( $fields as $index => $field ) {
if ( false !== strpos( $field->unique_name, 'shipping' ) || 'delivertoafriend' == $field->unique_name ) {
unset( $fields[ $index ] );