Skip to content

Fix TypeError: Cannot read properties of undefined (reading 'localeCompare')#368

Open
EarthmanWeb wants to merge 1 commit intoWordPress:trunkfrom
EarthmanWeb:fix/guard-wp-version-localecompare
Open

Fix TypeError: Cannot read properties of undefined (reading 'localeCompare')#368
EarthmanWeb wants to merge 1 commit intoWordPress:trunkfrom
EarthmanWeb:fix/guard-wp-version-localecompare

Conversation

@EarthmanWeb
Copy link

Summary

Fixes an Uncaught TypeError: Cannot read properties of undefined (reading 'localeCompare') thrown on every admin page load from scf-admin.min.js.

Problem

Both admin-commands.js and custom-post-type-commands.js read window.acf.data.wp_version and immediately call .localeCompare() on it to determine if the WordPress version is 6.9+. However, wp_version is only added to acf.data conditionally in includes/assets.php (line 622), inside a block that checks for Gutenberg/block editor context. On classic editor pages and other admin screens where that condition isn't met, wp_version is undefined, causing the TypeError.

Error:

Uncaught TypeError: Cannot read properties of undefined (reading 'localeCompare')
    at h (scf-admin.min.js?ver=6.8.0:1:6456)

This error fires on every admin page load via requestIdleCallback.

Fix

Added a fallback || '0' when reading window.acf.data.wp_version in both files:

// Before (crashes when wp_version is undefined)
const wpVersion = window.acf.data.wp_version;

// After (safely falls back to '0', treating unknown versions as pre-6.9)
const wpVersion = window.acf.data.wp_version || '0';

When wp_version is undefined, the fallback '0' ensures localeCompare('6.9') returns < 0, so all commands are registered (pre-6.9 behavior). This is the safe default — registering extra commands is harmless, while crashing is not.

Files Changed

  • assets/src/js/commands/admin-commands.js — Added || '0' fallback (line 216)
  • assets/src/js/commands/custom-post-type-commands.js — Added || '0' fallback (line 41)
  • tests/js/commands/admin-commands.test.js — Added test case for undefined wp_version
  • tests/js/commands/custom-post-type-commands.test.js — Added test case for undefined wp_version

Test Plan

  • Verified fix resolves the error on a production WordPress 6.9.1 site
  • Confirmed all existing commands still register correctly
  • npm run test:unit passes with new undefined version test cases
  • Verify on WP 6.9+ that view commands are correctly skipped when wp_version is available

Guard against `window.acf.data.wp_version` being undefined before
calling `localeCompare()` in both admin-commands.js and
custom-post-type-commands.js. Falls back to '0' which safely
registers all commands (pre-6.9 behavior).
@github-actions
Copy link

github-actions bot commented Mar 1, 2026

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 earthman100, bernhard-reiter.

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

@ockham
Copy link
Contributor

ockham commented Mar 9, 2026

Good spot, thank you!

As for the fix, I think it'd be better if we bailed early if window.acf.data.wp_version isn't available (rather than defaulting the WP version number to 0).

Turns out that we already have checks that look like they're supposed to cover cases like these:

if ( ! dispatch( 'core/commands' ) || ! window.acf?.data ) {
return;
}

and

// Only proceed when WordPress commands API and there are custom post types accessible
if (
! dispatch( 'core/commands' ) ||
! window.acf?.data?.customPostTypes?.length
) {
return;
}

They just don't seem to quite cover the case where wp_version isn't present 🤔
Maybe you can tweak those conditionals?

@ockham
Copy link
Contributor

ockham commented Mar 10, 2026

Looking a bit more into this had me discover a related/underlying issue: When the command palette is invoked (Cmd+K) from a non-editor wp-admin screen, SCF options aren't added there. (This is due to the conditionals I mentioned.)

Editor Other wp-admin
image image

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.

2 participants