Skip to content

Instantly share code, notes, and snippets.

@lgladdy
lgladdy / acf_enable_detailed_escape_logging_to_php_error_log.php
Last active April 8, 2024 08:48
Always log the post ID and value of a HTML escaped field
<?php
add_action( 'acf/will_remove_unsafe_html', 'acf_enable_detailed_escape_logging_to_php_error_log', 10, 4 );
add_action( 'acf/removed_unsafe_html', 'acf_enable_detailed_escape_logging_to_php_error_log', 10, 4 );
function acf_enable_detailed_escape_logging_to_php_error_log( $function, $selector, $field_object, $post_id ) {
if ( $function === 'the_sub_field' ) {
$field = get_sub_field_object( $selector, true );
$value = ( is_array( $field ) && isset( $field['value'] ) ) ? $field['value'] : false;
} else {
$value = get_field( $selector, $post_id );
}
@lgladdy
lgladdy / acf-unsafe-html-detection-backtrace.php
Last active January 30, 2024 17:23
Print backtrace on the frontend when potentially unsafe HTML is removed
<?php
add_action( 'acf/will_remove_unsafe_html', 'print_backtrace_for_unsafe_html_removal', 10, 4 );
add_action( 'acf/removed_unsafe_html', 'print_backtrace_for_unsafe_html_removal', 10, 4 );
function print_backtrace_for_unsafe_html_removal( $function, $selector, $field_object, $post_id ) {
echo '<h4 style="color:red">Detected Potentially Unsafe HTML Modification</h4>';
echo '<pre>';
debug_print_backtrace();
echo '</pre>';
}
@lgladdy
lgladdy / block.json
Last active November 24, 2023 12:36
An example ACF block.json which disables align controls, but forces a default to wide
{
"name": "automatic-wide-width",
"title": "Automatically Align Wide",
"description": "A demo block.json for a forced align-wide block",
"apiVersion": 2,
"acf": {
"mode": "edit",
"renderTemplate": "render.php"
},
"supports": {
@lgladdy
lgladdy / acf_blocks_parse_node_attr.js
Last active September 8, 2023 11:52
acf_blocks_parse_node_attr demo
acf.addFilter('acf_blocks_parse_node_attr', function( currentValue, nodeAttr ){
if (nodeAttr.name.startsWith('v-')) {
return { name: nodeAttr.name, value: nodeAttr.value };
}
return currentValue;
});
@lgladdy
lgladdy / custom-acf-json-paths.php
Last active January 11, 2024 16:09
ACF 6.2 custom load and save paths for ACF JSON
<?php
add_filter( 'acf/json/load_paths', 'set_custom_json_load_paths' );
function set_custom_json_load_paths( $paths ) {
$paths[] = get_stylesheet_directory() . '/acf-json/post-types';
$paths[] = get_stylesheet_directory() . '/acf-json/field-groups';
$paths[] = get_stylesheet_directory() . '/acf-json/taxonomies';
$paths[] = get_stylesheet_directory() . '/acf-json/option-pages';
return $paths;
}
@lgladdy
lgladdy / acf-shortcode-query-loop-support.php
Last active December 11, 2023 19:54
This code sample enables support for the ACF shortcode inside a query loop by overriding the default shortcode block renderer with one which will inject the correct Post ID into the shortcode parameters
<?php
add_filter( 'render_block_core/shortcode', 'test_acf_shortcode_render', 10, 3);
function test_acf_shortcode_render( $content, $parsed_block, $block ) {
$content = preg_replace_callback( '/\[acf\s.*?\]/', 'acf_inject_query_loop_post_ID', $content );
return $content;
}
function acf_inject_query_loop_post_ID( $match ) {
return str_replace( '[acf', '[acf post_id="' . get_the_ID() . '"', $match[0] );
//block.json
{
"name": "simple-test-block",
"title": "Simple Test Block",
"description": "Simple Test Block",
"category": "theme",
"icon": "admin-comments",
"acf": {
"mode": "preview",
"renderTemplate": "render.php"
@lgladdy
lgladdy / acf-use-editor-palette.js
Created July 22, 2021 07:02
Set the color picker palette to match the block editor's
acf.addFilter('color_picker_args', function (args) {
const settings = wp.data.select( "core/editor" ).getEditorSettings();
let colors = settings.colors.map(x => x.color);
args.palettes = colors;
return args;
});
@lgladdy
lgladdy / tailwind.config.js
Last active April 3, 2024 00:36
A tailwind plugin for generating WordPress block editor (Gutenberg) color css styles from the tailwind palette
const _ = require('lodash')
const plugin = require('tailwindcss/plugin')
plugins: [
plugin(function({ addComponents, theme }) {
const colors = theme('colors', {})
const exclude = ['transparent', 'current']
const paletteColors = []
for (const [key, value] of Object.entries(colors)) {
@lgladdy
lgladdy / assets.php
Last active August 23, 2018 13:52
Add zurb foundation widescreen/responsive-embed code to gutenberg video blocks.
function foundation_gutenberg_wrappers()
{
if (defined('WP_DEBUG') && WP_DEBUG) {
$gutenberg_file = '/js/raw/gutenberg.js?no_cache=' . time();
} else {
$gutenberg_file = '/js/raw/gutenberg.js';
}
wp_enqueue_script('foundation-gutenberg-extensions', get_template_directory_uri() . $gutenberg_file, array('wp-blocks'));
}
add_action('enqueue_block_editor_assets', 'foundation_gutenberg_wrappers');