AdvancedCustomFields / AdvancedCustomFields/acf

Custom Fields do not save when creating a new page

オープン
#653 コメント 5 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

主要言語
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:

  1. Create a new page.
  2. Set the title.
  3. Set the template to the one set for the custom field group
  4. Set the value of one of the fields in the field group after it appears.
  5. 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
);

コントリビューションガイド

このリポジトリのコントリビューションガイドは索引されていません

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. 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

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。