Skip to content

Instantly share code, notes, and snippets.

@MogulChris
MogulChris / script_run_once.php
Last active January 10, 2019 22:36
Ensure a PHP script can only have 1 (or N) instances running at a time
<?php
//Use PHP semaphores to ensure only one (or $max) instances of a script can run at once.
//Semaphores can auto-release if the script crashes, unlike a lock file or database flag which require the script to be functional to release them
//USAGE: Simply include this file at the top of your script. When run, it will die if it finds another instance of itself already running.
// include_once('/path/to/script_run_once.php');
//All we need is a unique 4-digit integer to represent our script. I will hash the current script file path to get this, but you can hard-code $key if you like
<?php
/*
* When using the 'pay for order' link with a WooCommerce Subscriptions renewal order, WCS recreates the order in the cart (setup_cart() in class-wcs-cart-renewal.php)
* However it does not copy custom order item meta to the cart items as cart item meta. This has to be done with the filter woocommerce_order_again_cart_item_data
*/
function my_custom_add_renewal_cart_item_data($cart_item_data, $line_item, $subscription){
//there will be only one set of cart item data, WCS just packages it as an array for this filter
$cart_item_data = array_shift($cart_item_data);
@MogulChris
MogulChris / acf_google_maps.php
Created January 23, 2019 02:24
Programmatically update ACF Google Maps fields
<?php
//You need an API key and an address (as a string) to geocode
//ACF Google Maps field data looks like:
/*
array(
'address' => '123 My Street, Mytown, Mycountry, Mypostcode',
'lat' => '123.45678',
'lng' => '-125.67890',
)
@MogulChris
MogulChris / cf7.php
Last active January 27, 2019 21:22
Contact Form 7 custom field values
<?php
/*
This goes in your functions.php or similar. It works the same for selects and radio buttons
In this example, we will provide dynamic values for a select named 'example-field1'
CF7 form tag: [select example-field1]
*/
function mytheme_cf7_dynamic_values($tag){
<?php
//Get a list of Mogul Framework layers to update the wiki
require_once('wp-load.php');
global $wpdb;
$args = array(
'post_type' => 'acf-field-group',
'posts_per_page' => -1,
@MogulChris
MogulChris / functions.php
Created February 13, 2019 01:39
Remove annoying Redux Framework notices and links
/**
* When used by other plugins, the Redux Framework adds obtrusive dashboard widgets, notices and menu items when wp_debug is enabled, on the assumption that nobody uses WP_DEBUG_LOG on production sites.
* This code removes these
*/
//Remove widget
function my_framework_remove_redux_stuff(){
global $wp_meta_boxes;
if(!empty($wp_meta_boxes['dashboard']['side']['high']['redux_dashboard_widget'])){
@MogulChris
MogulChris / wpdump.bash
Last active February 27, 2019 02:03 — forked from cyberhobo/wpdump.bash
Bash alias for mysqldump using credentials from a wp-config.php file
#!/bin/bash
# one argument: the directory of a wp-config.php file
wpconfig=$1/wp-config.php
if [ ! -f "$wpconfig" ]; then
echo "$wpconfig not found."
exit
fi
@MogulChris
MogulChris / wprestore.sh
Created February 27, 2019 02:32
wprestore.sh
#!/bin/bash
# one argument: the directory of a wp-config.php file
wpconfig=$1/wp-config.php
if [ ! -f "$wpconfig" ]; then
echo "$wpconfig not found."
exit
fi
@MogulChris
MogulChris / _plugins.php
Created March 4, 2019 21:35
Get a list of active Wordpress plugins, current versions and available versions
<?php
//based on https://wordpress.stackexchange.com/a/298323/88160 but standalone
require_once('wp-load.php');
ini_set("memory_limit","512M");
//error_reporting(E_ALL);
//ini_set('display_errors', 1);
@MogulChris
MogulChris / custom-acf-admin.js
Last active August 7, 2023 06:25 — forked from ChrisWebbNZ/hierearchical-select-acf.php
Hierarchical select for native Advanced Custom Fields taxonomy fields.
jQuery(document).ready(function($){
//Modify select2 ajax request to include a parent_id parameter, used by custom_acf_taxonomy_hierarchy()
acf.add_filter('select2_ajax_data', function( data, args, $input, field, instance ){
var target_field_key = 'field_5c7634ca3413f'; //YOUR TARGET FIELD KEY HERE
if(data.field_key == target_field_key){
var parent_id = 0; //by default we want terms with parent of 0 (top level)