Skip to content

Instantly share code, notes, and snippets.

@Idealien
Last active July 4, 2016 17:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Idealien/ed865e7dd271ec15310f6ccae52ebaa3 to your computer and use it in GitHub Desktop.
Save Idealien/ed865e7dd271ec15310f6ccae52ebaa3 to your computer and use it in GitHub Desktop.
[
{
"key": "group_575eae2ac9b8a",
"title": "TC",
"fields": [
{
"key": "field_575eae3ae007c",
"label": "text",
"name": "tc_text",
"type": "text",
"instructions": "",
"required": 1,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"default_value": "",
"placeholder": "",
"prepend": "",
"append": "",
"maxlength": "",
"readonly": 0,
"disabled": 0
},
{
"key": "field_575eae4ee007d",
"label": "select",
"name": "tc_select",
"type": "select",
"instructions": "",
"required": 1,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"choices": {
"Apple": "Apple",
"Banana": "Banana",
"Fig": "Fig",
"Grapefruit": "Grapefruit",
"Orange": "Orange"
},
"default_value": [
],
"allow_null": 0,
"multiple": 0,
"ui": 0,
"ajax": 0,
"placeholder": "",
"disabled": 0,
"readonly": 0
}
],
"location": [
[
{
"param": "post_type",
"operator": "==",
"value": "post"
}
]
],
"menu_order": 0,
"position": "normal",
"style": "default",
"label_placement": "top",
"instruction_placement": "label",
"hide_on_screen": "",
"active": 0,
"description": "",
"is_acf_component": 1
},
{
"key": "group_575eae805afaf",
"title": "TestFieldGroup",
"fields": [
{
"key": "field_575eaec66ee25",
"label": "Regular",
"name": "regular",
"type": "tab",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"placement": "top",
"endpoint": 0
},
{
"key": "field_575eaf186ee28",
"label": "Field Group Text",
"name": "fg_text",
"type": "text",
"instructions": "",
"required": 1,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"default_value": "",
"placeholder": "",
"prepend": "",
"append": "",
"maxlength": "",
"readonly": 0,
"disabled": 0
},
{
"key": "field_575eaf336ee29",
"label": "field Group Select",
"name": "fg_select",
"type": "select",
"instructions": "",
"required": 1,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"choices": {
"Apple": "Apple",
"Banana": "Banana",
"Date": "Date",
"Fig": "Fig",
"Grapefruit": "Grapefruit",
"Orange": "Orange"
},
"default_value": [
],
"allow_null": 0,
"multiple": 0,
"ui": 0,
"ajax": 0,
"placeholder": "",
"disabled": 0,
"readonly": 0
},
{
"key": "field_575eaedc6ee26",
"label": "Component",
"name": "_copy",
"type": "tab",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"placement": "top",
"endpoint": 0
},
{
"key": "field_575eaee36ee27",
"label": "Example Component",
"name": "example",
"type": "component_field",
"instructions": "",
"required": 1,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"field_group_id": "group_575eae2ac9b8a",
"min": 1,
"max": 1,
"layout": "block",
"button_label": "Add Row",
"appearances": "",
"collapsed": "",
"sub_fields": [
]
}
],
"location": [
[
{
"param": "post_type",
"operator": "==",
"value": "test"
}
]
],
"menu_order": 0,
"position": "normal",
"style": "default",
"label_placement": "top",
"instruction_placement": "label",
"hide_on_screen": "",
"active": 1,
"description": "",
"is_acf_component": 0
}
]
<?php
/* This version does not work with my environment */
//Front-End Post Meta / ACF and Revision Storage
add_filter('acf/pre_save_post', 'pre_save_post_update_revisions');
add_action('_wp_put_post_revision', '_on_wp_put_post_revision');
function pre_save_post_update_revisions($post_id)
{
// bail early if editing in admin
// ACF already handles admin revisions correctly
if (is_admin()) {
return $post_id;
}
// force a post update, this will generate a revision and
// trigger the '_wp_put_post_revision' action
wp_update_post(get_post($post_id));
// allow acf to update the parent post meta normally
return $post_id;
}
// revision just got created
// detect all of the acf fields on the original and
// apply them to the revision
function _on_wp_put_post_revision($revision_id)
{
// bail early if editing in admin
if (is_admin()) {
return;
}
// get the revision post object
$revision = get_post($revision_id);
// get the id of the original post
$post_id = $revision->post_parent;
// A lot of this is copied from ACF's revision class
// get all the post meta from the original post
$custom_fields = get_post_custom($post_id);
if (!empty($custom_fields)) {
foreach ($custom_fields as $k => $v) {
// value is always an array
$v = $v[0];
// bail early if $value is not is a field_key
if (!acf_is_field_key($v)) {
continue;
}
// remove prefix '_' field from reference
$field_name = substr($k, 1);
// update the field value using the POST value supplied in the form
//error_log("-------------------------- Attempted Save");
//error_log(print_r($_POST['acf'][$v], true) );
if (!is_admin()) {
update_field($field_name, $_POST['acf'][$v], $revision_id);
// add the reference key
update_metadata('post', $revision_id, $k, $v);
}
}
}
}
<?php
add_filter('acf/pre_save_post', 'pre_save_post_update_revisions');
function pre_save_post_update_revisions($post_id)
{
// bail early if editing in admin
// ACF already handles admin revisions correctly
if (is_admin()) {
return $post_id;
}
// force a post update, this will generate a revision and
// trigger the '_wp_put_post_revision' action
wp_save_post_revision(get_post($post_id));
//wp_update_post(get_post($post_id));
return $post_id;
}
add_action('_wp_put_post_revision', '_on_wp_put_post_revision');
// revision just got created, detect all of the acf fields on the original and apply them to the revision
function _on_wp_put_post_revision($revision_id) {
// bail early if editing in admin
if (is_admin())
return;
// get the revision post object
$revision = get_post($revision_id);
// get the id of the original post
$post_id = $revision->post_parent;
if (!empty($_POST['acf'])) {
foreach ($_POST['acf'] as $k => $v) {
// bail early if $value is not is a field_key
if (!acf_is_field_key($k)) {
continue;
}
update_field($k, $v, $revision_id);
update_metadata('post', $revision_id, $k, $v);
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment