Skip to content

Instantly share code, notes, and snippets.

View jondcampbell's full-sized avatar

Jon Campbell jondcampbell

View GitHub Profile
@jondcampbell
jondcampbell / functions.php
Created July 15, 2016 18:56
updating data on wp all import save
add_action( 'pmxi_saved_post', 'sgc_post_saved', 10, 1 );
function sgc_post_saved( $post_id ) {
// We only want to run this on import of course members
if ( 'course-members' === get_post_type( $post_id ) ) :
// Perform proper date formatting on our date of birth field, and passport dates
update_date_value_of_post( $post_id, 'personal_date_of_birth' );
update_date_value_of_post( $post_id, 'travel_passport_date_of_issue' );
@jondcampbell
jondcampbell / class-wc-emails.php
Created September 23, 2020 18:50
low stock notification change
/**
* Determine if the current product should trigger a low stock notification
*
* @param int $product_id - The low stock product id
*
* @since 4.4.0
*/
if ( false === apply_filters( 'woocommerce_should_send_low_stock_notification', true, $product->get_id() ) ) {
return;
}
@jondcampbell
jondcampbell / functions.php
Created August 14, 2020 16:36
Limit length of first name field in Gravity Forms. Applies to ALL FORMS.
add_filter( 'gform_field_validation', 'kuztek_first_name_validation', 10, 4 );
function kuztek_first_name_validation( $result, $value, $form, $field ) {
$first_name_max_length = 10;
$first_name = rgar( $value, $field->id . '.3' );
if ( $result['is_valid'] && 'name' === $field->type && strlen( $first_name ) > $first_name_max_length ) {
$result['is_valid'] = false;
$result['message'] = 'Please enter a first shorter name';
}
return $result;
}
@jondcampbell
jondcampbell / template.php
Created October 20, 2014 17:46
Get product brand thumbnails in a random order. Replaces [product_brand_thumbnails]
<?php
// This code is basically taken out of the product_brand_thumbnails shortcode and then we add in some shuffling of the order
$brands = get_terms( 'product_brand', array( 'hide_empty' => 0, 'orderby' => 'name', 'exclude' => '', 'number' => 5, 'order' => 'ASC' ) );
if ($brands ):
$columns = 5;
// Randomly order the brands
shuffle($brands);
?>
@jondcampbell
jondcampbell / class-cron-pull.php
Created February 26, 2019 06:19
WordPress Cron running in a php class on a custom schedule
<?php namespace MyProject;
/**
* Cron Pull
*
* Cron functionality for pulling data on a regular basis
*
* @package MyProject
*/
use WP_Query;

Familiar Tools

A way for me to remember what my go-to libraries and utilities are.

PHP

Working with APIs

  • HTTP Requests - Guzzle
  • Logging - Monolog

JavaScript

VueJS

@jondcampbell
jondcampbell / functions.php
Created April 25, 2018 23:16
WordPress log anything
function logger($log){
if ( is_array( $log ) || is_object( $log ) ) {
error_log( print_r( $log, true ) );
} else {
error_log( $log );
}
@jondcampbell
jondcampbell / functions.php
Created April 10, 2018 23:11
WooCommerce order email billing country
/*
* Let's add some more information to the customer details block in admin emails
* This particular example adds the billing country
*/
function mysite_filter_woocommerce_email_customer_details_fields( $fields, $sent_to_admin, $order ) {
if ( $order->billing_country ) {
$fields['billing_country'] = array(
'label' => __( 'Country', 'woocommerce' ),
'value' => WC()->countries->countries[ $order->billing_country ],
@jondcampbell
jondcampbell / email-order-details.php
Created April 10, 2018 22:55
Adding Moneris credit card type to woocommerce order emails
<?php
/*
* This would fit into yourtheme/woocommerce/emails/email-order-details.php and replace the existing loop of totals in there.
*/
if ( $totals = $order->get_order_item_totals() ) {
$i = 0;
foreach ( $totals as $key => $total ) {
$i++;
@jondcampbell
jondcampbell / cache-functions.php
Created April 5, 2018 20:53
Clear some WP Super Cache files when any post is saved
<?php
/**
* Functionality for managing WP Super Cache
*/
/**
* On post save
*