Skip to content

Instantly share code, notes, and snippets.

View 0-woocommerce-snippets.md

WooCommerce Snippets

Just a handy collection of small snippets for customising WooCommerce.

View localize-script-variables-for-wordpress.php
<?php
add_action('wp_enqueue_scripts', function(){
// Assuming our previous example code is in a script enqueued with the handle `my-script` e.g;
// wp_enqueue_script( 'my-script', … );
// Localise an object containing our nonce:
wp_localize_script( 'my-script', 'myvars', [
'nonce' => wp_create_nonce( 'wp_rest' )
@mishterk
mishterk / 0-readme.md
Last active Jan 6, 2022
How to ensure post data from the Gravity Forms Advanced Post Creation Add-on is stored in ACF Custom Database Tables correctly.
View 0-readme.md

Using ACF Custom Database Tables in conjunction with the Gravity Forms Advanced Post Creation Add-on

The Gravity Forms post creation add-on doesn't appear to use ACF's API functions in order to save post meta on newly created posts. It's likely using core meta functions such as update_post_meta() instead of ACF's update_field() and in doing so, the ACF Custom Database Tables plugin doesn't have a chance to intercept the call and store the data in the appropriate custom database table.

In order to make the plugins work together, it's necessary to hook into the post creation process and save the field values using ACF's update_field() function. This will allow ACF Custom Database Tables to locate the appropriate custom DB table and store data there.

An example of this is provided in the below snippets and is based on the Gravity Forms documented approach [here](https://docs.gra

@mishterk
mishterk / SimpleWooRegistrationSpamFilter.php
Created Nov 24, 2021
A simple, custom SPAM filter for WooCommerce registration forms. This just prevents registration form submissions unless the custom field has the expected value in it.
View SimpleWooRegistrationSpamFilter.php
<?php
namespace PhilKurth;
use WP_Error;
/**
* Class adds a simple math equation to the registration form in order to help combat SPAM submissions.
*/
class SimpleWooRegistrationSpamFilter {
View generate-acf-json-files-for-all-field-groups-on-request.php
<?php
add_action( 'admin_post_acf_sync', function () {
$field_groups = acf_get_field_groups();
// Apply our callback to all field groups
array_map( function ( $field_group ) {
// Load up the fields on the field group.
@mishterk
mishterk / acf-php-license-config.php
Last active Jun 3, 2022
How to define the ACF PRO license key in code rather than storing it in the WordPress database. This Gist accompanies the article: https://www.awesomeacf.com/snippets/store-acf-pro-license-key-in-wordpress-php-configuration/
View acf-php-license-config.php
<?php
// Add this in your wp-config.php file
define('ACF_PRO_LICENSE', '{YOUR LICENSE KEY HERE}' );
View how-to-update-acf-google-map-field.php
<?php
// The field accepts a value with this structure
$value = [
'address' => '123 Example St, Townsville XYZ 1234, Country',
'lat' => - 38.1486228,
'lng' => 144.360414,
'zoom' => 14,
'place_id' => 'Ei0xMjMgTW9vcmFib29sIFN0LCBHZWVsb25nIFZJQyAzMjIwLCBBdXN0cmFsaWEiMBIuChQKEgmX0JaIHBTUahFyH_LC9sYD8hB7KhQKEglDz-DYDxTUahECZY8QisCjzg',
'street_number' => 123,
View wordpress-rest-api-simple-endpoint.php
<?php
add_action( 'rest_api_init', function () {
register_rest_route( 'my-namespace/v1', 'some/route', [
'methods' => [ 'GET' ],
'permission_callback' => '__return_true',
'callback' => function () {
// You can return arrays or objects here — WordPress will
// format them for output as JSON.
@mishterk
mishterk / exclude-multiple-fields-using-function.php
Last active Nov 2, 2021
How to exclude fields from rendering in ACF front end forms when using Advanced Forms Pro. Code snippets accompany the post: https://hookturn.io/2021/11/exclude-fields-in-acf-front-end-forms/
View exclude-multiple-fields-using-function.php
<?php
advanced_form( 'form_61806dc10891f', [
'exclude_fields' => [
'field_61806e15ca267',
'email',
]
] );
@mishterk
mishterk / functions.php
Last active Oct 17, 2021
A simple example of using filemtime() to version enqueued assets with their last modified time. Snippets for https://hookturn.io/2021/10/automatic-versioning-strategy-for-enqueued-wordpress-assets/
View functions.php
<?php
// A simple example of using filemtime() to version enqueued assets with their
// last modified time.
add_action( 'wp_enqueue_scripts', function () {
wp_register_style(
'styles',
get_stylesheet_directory_uri() . '/style.css',
[],