Skip to content

fix(export): register post meta in PHP export - #544

Open
faisalahammad wants to merge 2 commits into
WordPress:trunkfrom
faisalahammad:fix/384-export-post-meta
Open

faisalahammad wants to merge 2 commits into
WordPress:trunkfrom
faisalahammad:fix/384-export-post-meta

Conversation

@faisalahammad

@faisalahammad faisalahammad commented Sep 22, 2026 •

Copy link
Copy Markdown

Generated PHP now includes register_post_meta() calls for fields that are eligible, so exported field groups work with block bindings in block themes without manual registration. Previously the export only emitted acf_add_local_field_group() and register_post_type(), so block bindings reported the field as not registered and it did not show in the bindings picker.

Closes #384

What gets registered

A field is exported as post meta only when all of these are true:

  • The field group has Show in REST API enabled.
  • The field group targets a concrete post type, meaning every location group is a single post_type == <slug> rule. Groups with mixed or broader rules are skipped rather than guessed.
  • The field is not disabled for bindings, and its field type supports bindings.
  • The field type is REST enabled.
  • The field resolves to exactly one scalar REST type (string, boolean, integer, number). Multi value fields such as checkbox, repeater, gallery, or relationship are skipped, since they cannot be registered as single native meta safely.
  • The field name is not a protected meta key (no leading underscore).

The output is added to the existing export as a separate block:

add_action( 'init', function() {
	register_post_meta( 'book', 'book_title', array(
		'single'       => true,
		'type'         => 'string',
		'show_in_rest' => true,
	) );
} );

The existing acf_add_local_field_group() output is unchanged, so this is additive.

Files changed

  • includes/acf-field-group-functions.php - helper functions to resolve concrete post types from location rules, decide which fields can be registered, and build the exported PHP.
  • includes/admin/tools/class-acf-admin-tool-export.php - prints the post meta block alongside the field group export.
  • tests/php/includes/test-acf-field-group-functions.php - tests for scalar export, multiple post types, skipped fields, and ambiguous locations.

How to test

  1. Create a field group on a custom post type, for example a book post type.
  2. Enable Show in REST API for the group and add a text field.
  3. Use Tools > Export to generate PHP for that group.
  4. Confirm the output contains a register_post_meta( 'book', ... ) call for the field.
  5. Load the exported PHP and open a block theme template. The field should appear in the Block Bindings attributes list.

Testing done

  • composer test:php passes: 2947 tests, 22753 assertions.
  • composer lint:php and the phpcs changed lines check pass on the changed files.
  • Full build runs clean.
  • Manually verified the exported PHP loads and the field appears in the Block Bindings picker.

Use of AI Tools

This pull request was developed with assistance from GLM 5.3. The tool was used for repository investigation, implementation support, test planning, and review. The final code, tests, and submitted changes were reviewed and verified by the contributor.

Exported field groups now include register_post_meta() calls for eligible
fields, so exported fields work with block bindings in block themes
without manual registration.

Fixes WordPress#384
@github-actions

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props faisalahammad, jhmonroe.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

Pass explicit flags to html_entity_decode() so single quote
entities decode the same on PHP 7.4 and 8.x. Match the editor
preview menu item with a tolerant pattern because WP trunk
renamed it to Preview (opens in a new tab).

Refs WordPress#544
@faisalahammad

Copy link
Copy Markdown
Author

CI fixes for the failing checks: f9fb7ce

File Error Fix
tests/php/includes/test-acf-field-group-functions.php PHP 7.4 run failed. The test decoded the export with html_entity_decode() and no flags. PHP 7.4 does not decode single quotes by default, PHP 8.x does, so the assertion only passed on newer PHP. Pass ENT_QUOTES and UTF-8 so the decode works the same on all versions. Production code still escapes with esc_textarea(), only the test changed.
tests/e2e/fixtures.js Three E2E shards failed on WordPress trunk. They timed out waiting for the preview page. Trunk renamed the editor menu item from "Preview in new tab" to "Preview (opens in a new tab)", so the older label no longer matched. Match the menu item with a tolerant pattern that covers both old and new labels. WP 6.2, 6.3+ and trunk now work.

The E2E change also fixes the editor preview fixture on trunk for other PRs, since trunk E2E started failing after the label rename.

Refs #384

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Exported PHP doesn't register post_meta

1 participant