Skip to content

Instantly share code, notes, and snippets.

@mishterk
mishterk / SimpleWooRegistrationSpamFilter.php
Created November 24, 2021 21:28
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.
<?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 {
<?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 June 3, 2022 19:33
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/
<?php
// Add this in your wp-config.php file
define('ACF_PRO_LICENSE', '{YOUR LICENSE KEY HERE}' );
<?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,
@mishterk
mishterk / wordpress-rest-api-simple-endpoint.php
Last active November 3, 2021 05:24
WordPress REST API examples
<?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 November 2, 2021 00:06
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/
<?php
advanced_form( 'form_61806dc10891f', [
'exclude_fields' => [
'field_61806e15ca267',
'email',
]
] );
@mishterk
mishterk / functions.php
Last active October 17, 2021 20:55
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/
<?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',
[],
@mishterk
mishterk / custom-wordpress-rest-api-url.php
Created October 14, 2021 02:00
How to customise the WordPress REST API URL prefix (wp-json). This snippet is for the https://hookturn.io/2021/10/how-to-customise-the-wordpress-rest-api-url-prefix-wp-json/ blog post.
<?php
// This will replace the 'wp-json' REST API prefix with 'api'.
// Be sure to flush your rewrite rules for this change to work.
add_filter( 'rest_url_prefix', function () {
return 'api';
} );
@mishterk
mishterk / load-flexi-field-layout-partials-in-templates.php
Created September 17, 2021 04:46
Split an ACF flexible content field into template parts
<?php
// In a post/page template, loop over the ACF flexible field layouts and load the partial
// responsible for rendering the layout.
while ( the_flexible_field('my_flexi_field') ) {
get_template_part( 'components/'. get_row_layout() );
}
@mishterk
mishterk / disable-core-meta-metabox.php
Created September 17, 2021 04:41
Speed up ACF backend loading time
<?php
add_filter('acf/settings/remove_wp_meta_box', '__return_true');