Skip to content

Instantly share code, notes, and snippets.

@MogulChris
MogulChris / screenshot.php
Created April 10, 2019 22:15
Automation and taking full-screen screenshots using Laravel Dusk and Chrome webdriver
<?php
/**
* This script is using Laravel Dusk to take screenshots of a URL, but not in the context of a regular Dusk test.
* My Laravel application is taking screenshots of *other* sites, but you could adapt this to a Dusk test taking screenshots of your Laravel site.
* 1. Install Dusk https://laravel.com/docs/5.8/dusk#installation
* 2. Make sure it works with regular Dusk tests first.
* 3. Use the code below for a stand-alone Chrome webdriver instance to do automation things.
*/
@MogulChris
MogulChris / new_zealand_regions.json
Created May 5, 2019 21:41
List of New Zealand regions in JSON format
var regions = [
"northland" => "Northland",
"auckland" => "Auckland",
"waikato" => "Waikato",
"bay_of_plenty" => "Bay of Plenty",
"gisborne" => "Gisborne",
"hawkes_bay" => "Hawke's Bay",
"taranaki" => "Taranaki",
"manawatu_wanganui" => "Manawatu-Wanganui",
"wellington" => "Wellington",
@MogulChris
MogulChris / functions.php
Last active May 28, 2019 02:39
Wordpress - add custom columns to the listing pages for one or post types
<?php
//easy way to cover multiple post types, including custom ones
foreach(['page','post','custom_post_type'] as $content_type){
add_filter( 'manage_' . $content_type . '_posts_columns', 'mytheme_custom_column' );
add_action( 'manage_' . $content_type . '_posts_custom_column', 'mytheme_custom_column_values', 10, 2);
add_filter( 'manage_edit-' . $content_type . '_sortable_columns', 'mytheme_custom_column_sorting' );
}
//column label
@MogulChris
MogulChris / functions.php
Created May 29, 2019 04:48
Easy WooCommerce global store discounts
<?php
//We will use this function to modify prices whenever WooCommerce retrieves them
function mytheme_global_sale_price( $price, $product ) {
$product_id = $product->get_id();
//do any checks you need to exclude or include products here
$excluded_products = [123,678];
if(in_array($product_id, $excluded_products)) return $price;
@MogulChris
MogulChris / theme-scripts.php
Last active November 5, 2021 12:14
Automatic versioning with WordPress CSS and JS files (avoid hard refresh problems)
<?php
//When registering styles and scripts in WordPress, you can specify a 'version' parameter which is appended to the file's URL.
//This is fine, but it's inconvenient having to bump the version number whenever you make a change. So why not do it automatically?
//Note we use get_stylesheet_directory() instead of get_stylesheet_directory_uri(), so we get an absolute file path for filemtime()
$mtime = filemtime(get_stylesheet_directory() .'/js/script.js');
wp_register_script( 'theme-js', get_stylesheet_directory_uri() .'/js/script.js', array( 'jquery' ), $mtime, true );
wp_enqueue_script( 'theme-js' );
@MogulChris
MogulChris / gist:8fa67eedf01ef759e92e33a8e784aaa0
Created October 15, 2019 20:25
WooCommerce programmatically create variations from all attributes
<?php
/*
WooCommerce has a handy feature in the admin UI to "Create variations from all attributes".
To do this programmatically (as of WooCommerce v3.7.0) use a modified version of link_all_variations() found in woocommerce/includes/class-wc-ajax.php
*/
function myplugin_create_variations($product_id){
wc_maybe_define_constant( 'WC_MAX_LINKED_VARIATIONS', 50 );
wc_set_time_limit( 0 );
@MogulChris
MogulChris / functions.php
Created November 4, 2019 22:17
Default values for ACF flexible content fields
<?php
//Set a default layout to appear in a Flexible Content Field for certain post types (eg products)
//We just set a default value for our desired field. This is an array of arrays.
//Each inner array requires at minimum an element called 'acf_fc_layout'. You may also set other field default values here (use field keys, not names).
function mytheme_set_default_layout($value, $post_id, $field){
if(get_post_type($post_id) == 'product' && empty($value)){
$value = array(
@MogulChris
MogulChris / functions.php
Last active July 31, 2022 23:37
Hooking into the native WooCommerce product import
<?php
//Since 3.x WooCommerce has had an excellent native product import/export.
//You can do a huge amount just by tweaking your CSV to load meta values into the correct meta fields for your destination site.
//It'll even import images from the source site by URL if possible, and upload them to the destination site.
//If you need to do something a little fancier for every product you import, check out the woocommerce_product_import_* hooks here https://woocommerce.github.io/code-reference/hooks/hooks.html
//You can modify the CSV data before it's used, and modify the resulting WC_Product during and after creation
//eg do something to every product after it's created
@MogulChris
MogulChris / mytheme_shipping_plugin.php
Created November 21, 2019 00:18
WooCommerce custom shipping method plugin
<?php
//I found a few tutorials for making custom WooCommerce shipping methods but most were confusing or didn't work any more.
//This is a simple custom shipping method.
//It originally calculated a fee per 'delivery day' based on some date meta on the cart items, but for this example it's a hard-coded flat fee.
//See calculate_shipping()
//Step 1:
//Save this code (minus these instructions) in a file in your plugins directory, preferably in its own subdirectory eg myplugin_shipping/myplugin_shipping.php
//Step 2:
@MogulChris
MogulChris / functions.php
Last active October 31, 2023 03:36
Programmatically import images into WordPress with PHP
<?php
/**
* Grab a remote image and import it into WordPress with PHP
*/
function mytheme_import_image($image_url){
//$image_url is something like https://www.mysite.com/images/myimage.jpg