Skip to content

Instantly share code, notes, and snippets.

@MadtownLems
Created November 15, 2022 20:31
Show Gist options
  • Save MadtownLems/d799a438cf0fb9c724d4931fd59d9462 to your computer and use it in GitHub Desktop.
Save MadtownLems/d799a438cf0fb9c724d4931fd59d9462 to your computer and use it in GitHub Desktop.
ACF (Classic) Widget Proof of Concept
<?php
/*
Plugin Name: ACF Widget - save dialog, proof of concept
Description: Demonstrates a bug when required ACF field groups are used in a widget (See comment after docblock)
Version: 1
Author: Jason LeMahieu
Author URI: https://jasonlemahieu.com/
*/
/*
When this plugin is active (whether or not the widget is even being used), the following issue occurs on the Classic Widgets screen
Making any change to any widget, and saving it, will cause the pop-up dialog to warn you that you might lose changes (on Chrome)
"Leave site? Changes you made may not be saved."
This only happens when the text field is Required.
Note that the following is in the network panel after saving ANY widget:
admin-ajax.php response:
{"success":true,"data":{"valid":0,"errors":[{"input":"widget-my_widget[__i__][acf][field_6373df44fae11]","message":"Test Text Input value is required"}]}}
*/
class ACF_Widget extends WP_Widget {
/**
* Sets up the widgets name etc
*/
public function __construct() {
$widget_ops = array(
'classname' => 'my_widget',
'description' => 'My Widget is awesome',
);
parent::__construct( 'my_widget', 'ACF Test Widget', $widget_ops );
}
/**
* Outputs the content of the widget
*
* @param array $args
* @param array $instance
*/
public function widget( $args, $instance ) {
// outputs the content of the widget
/* Handled by Advanced Custom Fields */
}
/**
* Outputs the options form on admin
*
* @param array $instance The widget options
*/
public function form( $instance ) {
// outputs the options form on admin
/* Handled by Advanced Custom Fields */
}
/**
* Processing widget options on save
*
* @param array $new_instance The new options
* @param array $old_instance The previous options
*
* @return array
*/
public function update( $new_instance, $old_instance ) {
// processes widget options to be saved
}
}
add_action( 'widgets_init', function(){
register_widget( 'ACF_Widget' );
});
if( function_exists('acf_add_local_field_group') ):
acf_add_local_field_group(array(
'key' => 'group_6373df52d6642',
'title' => 'ACF - Widget Proof of Concept',
'fields' => array(
array(
'key' => 'field_6373df44fae11',
'label' => 'Test Text Input',
'name' => 'test_text_input',
'aria-label' => '',
'type' => 'text',
'instructions' => '',
'required' => 1,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'user_roles' => array(
0 => 'all',
),
'default_value' => '',
'maxlength' => '',
'placeholder' => '',
'prepend' => '',
'append' => '',
),
),
'location' => array(
array(
array(
'param' => 'widget',
'operator' => '==',
'value' => 'my_widget',
),
),
),
'menu_order' => 0,
'position' => 'normal',
'style' => 'default',
'label_placement' => 'top',
'instruction_placement' => 'label',
'hide_on_screen' => '',
'active' => true,
'description' => '',
'show_in_rest' => 0,
));
endif;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment