Skip to content

Instantly share code, notes, and snippets.

@mishterk
mishterk / wp-query-efficiency-args.php
Created Jun 24, 2021
Some handy considerations when running WP_Query to speed up the query depending on the requirements. This demonstrates using specific post ID's from ACF custom database tables in conjunction with some built in WP_Query args to save on internal queries.
View wp-query-efficiency-args.php
<?php
$query = new WP_Query([
// Standard query args. Using post__in can be much faster. If searching ACF custom
// database tables data, plugin the found post IDs in here.
'post_type' => 'post',
'post__in' => [1,2,3], // array of post IDs
// Optional args to improve performance. Use these to cut down on internal
@mishterk
mishterk / clone-gutenberg-reusable-block.php
Last active May 3, 2021
A function that clones an existing reusable block a given number of times.
View clone-gutenberg-reusable-block.php
<?php
/**
* Clone a given reusable block a given number of times.
*
* @param int $block_id The ID of an existing reusable block to clone.
* @param int $n_clones The number of clones to create.
*/
function clone_reusable_block( $block_id, $n_clones ) {
$post = get_post( $block_id );
@mishterk
mishterk / change-the-response-type-to-a-redirect.js
Last active Apr 30, 2021
AJAX functionality examples for Advanced Forms Pro version 1.8.0
View change-the-response-type-to-a-redirect.js
jQuery(function ($) {
// Changing the response type to a redirect on the fly.
acf.addAction('af/form/ajax/submission', function (data, form) {
if (form.key === 'form_5f8f987654e0') {
data.type = 'redirect';
data.redirect_url = 'https://example.com';
}
});
});
@mishterk
mishterk / 0-readme.md
Last active Dec 30, 2020
Dynamically set the ACF JSON directory so that each sub-site in a WordPress multisite network has a separate acf-json dir. This maximises compatibility with ACF Custom Database Tables.
View 0-readme.md

Using ACF JSON on WordPress multisite installations

ACF JSON can get a little messy where you need different field groups across your sub-sites. If you are using separate themes or separate child themes for each sub-site, you'll be fine as long as each theme/child-theme has its own /acf-json directory.

If, however, you want to enforce unique acf-json directories for each sub-site, you can do so using one of the following snippets.

  1. Dynamic ACF JSON directory
@mishterk
mishterk / PostTypeTaxonomyMirror.php
Created Dec 17, 2020
Post type/taxonomy mirror.
View PostTypeTaxonomyMirror.php
<?php
class PostTypeTaxonomyMirror {
private $post_type;
private $taxonomy;
@mishterk
mishterk / wplk-get-current-domain-id.php
Last active Dec 12, 2020
An example of getting the current domain post ID in WP Landing Kit. Might be of use if you are loading up ACF fields on a domain post or something similar.
View wplk-get-current-domain-id.php
<?php
// e.g; 'mydomain.com'
$current_host = $_SERVER['HTTP_HOST'];
// This will return a WPLK_Domain object, if the domain can be found. Otherwise, NULL.
$domain = wplk_get_domain( );
if($domain){
$domain_id = $domain->post_id();
@mishterk
mishterk / sql-in-wordpress-example-01.sql
Last active Dec 7, 2020
In A beginner's guide to using SQL to query the WordPress database — https://hookturn.io/2020/12/custom-wordpress-sql-queries-for-beginners/ — we take a look at the basics of SQL and how to use it within WordPress safely.
View sql-in-wordpress-example-01.sql
SELECT * FROM some_table_name;
@mishterk
mishterk / acfcdt-prefix-post-title-with-meta-id.php
Created Nov 26, 2020
Prefixing a WordPress post title with the ID of an ACF Custom Database Tables meta data row.
View acfcdt-prefix-post-title-with-meta-id.php
<?php
add_action( 'acf/save_post', function ( $post_id ) {
global $wpdb;
// Get the meta ID from the custom DB table.
$table_name = 'my_custom_db_table';
$meta_id = $wpdb->get_var( $wpdb->prepare( "SELECT `id` FROM {$wpdb->prefix}{$table_name} WHERE `post_id` = %d", $post_id ) );
// Choose a prefix format.
@mishterk
mishterk / advanced-forms-pro-prefill-fields-using-custom-data.php
Created Oct 21, 2020
How to use ACF Forms to manage non ACF controlled meta fields with Advanced Forms Pro.
View advanced-forms-pro-prefill-fields-using-custom-data.php
<?php
add_filter( 'af/field/prefill_value', 'prefill_form_field', 10, 4 );
/**
* @param mixed $value
* @param array $field
* @param array $form
* @param array $args
*
@mishterk
mishterk / advanced-forms-pro-dynamic-user-editing.php
Last active Oct 21, 2020
Using a custom query parameter to dynamically specify a user ID for editing with Advanced Forms Pro.
View advanced-forms-pro-dynamic-user-editing.php
<?php
add_filter( 'af/form/args', 'afp_demo_support_dynamic_user_editing', 10, 2 );
/**
* @param array $args
* @param array $form
*
* @return array
*/