AdvancedCustomFields / AdvancedCustomFields/acf
Custom Fields do not save when creating a new page
还没有人认领这个 Issue。
- 主要语言
- PHP
- 星标
- 945
- 派生
- 197
- PR 合并指标
- 30 天内没有已合并 PR
描述
I have a custom field group that conditionally appears for pages that use a specific template. I do the following:
- Create a new page.
- Set the title.
- Set the template to the one set for the custom field group
- Set the value of one of the fields in the field group after it appears.
- Publish the page.
If I then refresh the editor page or view the published page, the value set in the field has not been saved.
I dug into this a bit and discovered that WordPress caches the added metaboxes on initial page load and does not check again when publishing. So, if there were no metaboxes when first creating the page, it will not attempt to save any metaboxes when publishing. ACF appears to do the correct thing and register the metaboxes when the conditions are met, but a bug in WordPress (which they may or may not want to fix) does not detect the newly added metaboxes.
To work around this for right now, I basically duplicated the code from WordPress and updated it to check if there are metaboxes every time it detects a save was just requested:
(function (
external_wp_data_,
external_wp_editor_
) {
/*
CUSTOMIZATION: MODIFICATION
ORIGINAL:
let wasSavingPost = yield external_wp_data_["controls"].select(external_wp_editor_["store"], 'isSavingPost');
let wasAutosavingPost = yield external_wp_data_["controls"].select(external_wp_editor_["store"], 'isAutosavingPost');
const hasMetaBoxes = yield external_wp_data_["controls"].select(store, 'hasMetaBoxes'); // Save metaboxes when performing a full save on the post.
NEW:
*/
let wasSavingPost = Object(external_wp_data_["select"])(external_wp_editor_["store"]).isSavingPost();
let wasAutosavingPost = Object(external_wp_data_["select"])(external_wp_editor_["store"]).isAutosavingPost();
/* CUSTOMIZATION: END */
Object(external_wp_data_["subscribe"])(() => {
const isSavingPost = Object(external_wp_data_["select"])(external_wp_editor_["store"]).isSavingPost();
const isAutosavingPost = Object(external_wp_data_["select"])(external_wp_editor_["store"]).isAutosavingPost(); // Save metaboxes on save completion, except for autosaves that are not a post preview.
//
// Meta boxes are initialized once at page load. It is not necessary to
// account for updates on each state change.
//
// See: https://github.com/WordPress/WordPress/blob/5.1.1/wp-admin/includes/post.php#L2307-L2309
/*
CUSTOMIZATION: MODIFICATION
ORIGINAL:
const shouldTriggerMetaboxesSave = hasMetaBoxes && wasSavingPost && !isSavingPost && !wasAutosavingPost; // Save current state for next inspection.
NEW:
*/
const shouldTriggerMetaboxesSave = (
wasSavingPost
&&
!isSavingPost
&&
!wasAutosavingPost
&&
Object(external_wp_data_["select"])(wp.editPost["store"]).hasMetaBoxes()
);
/* CUSTOMIZATION: END */
wasSavingPost = isSavingPost;
wasAutosavingPost = isAutosavingPost;
if (shouldTriggerMetaboxesSave) {
Object(external_wp_data_["dispatch"])(wp.editPost["store"]).requestMetaBoxUpdates();
}
});
})(
window.wp.data,
window.wp.editor
);
贡献指南
这个仓库没有索引到贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
复现 issue 中描述的条件自定义字段工作流,然后首先将 metabox 的保存行为与 WordPress 的 wp-admin/includes/post.php 第 2307-2309 行进行比较。使用提供的 JavaScript workaround 作为参考案例,并验证页面初始加载后出现的字段会在页面发布时保存。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- javascript, php
- 领域
- content, web-dev
- Issue 类型
- 缺陷
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 38/100