Skip to content

Add command palette commands to navigate to options pages#514

Open
faisalahammad wants to merge 1 commit into
WordPress:trunkfrom
faisalahammad:enhancement/140-command-palette-options-pages
Open

Add command palette commands to navigate to options pages#514
faisalahammad wants to merge 1 commit into
WordPress:trunkfrom
faisalahammad:enhancement/140-command-palette-options-pages

Conversation

@faisalahammad

@faisalahammad faisalahammad commented Jul 21, 2026

Copy link
Copy Markdown

Closes #140

Summary

Each registered SCF options page now gets its own Command Palette command, so users can jump straight to a page by typing its name (Cmd/Ctrl+K) instead of clicking through the admin menu. The command is filtered by capability on the server, so users only see pages they can open.

Changes

includes/admin/admin-commands.php

Before:

if ( current_user_can( acf_get_setting( 'capability' ) ) ) {
    wp_enqueue_script( 'scf-commands-admin' );
}

After:

if ( current_user_can( acf_get_setting( 'capability' ) ) ) {
    wp_enqueue_script( 'scf-commands-admin' );

    $pages = acf_get_options_pages();
    if ( ! empty( $pages ) ) {
        $localized = array();
        foreach ( $pages as $page ) {
            if ( ! current_user_can( $page['capability'] ) ) {
                continue;
            }
            $localized[] = array(
                'menu_slug'  => $page['menu_slug'],
                'menu_title' => $page['menu_title'],
                'page_title' => $page['page_title'],
            );
        }
        if ( ! empty( $localized ) ) {
            wp_localize_script( 'scf-commands-admin', 'scfOptionsPages', $localized );
        }
    }
}

Why: Ships the page list to the admin command script. The capability check runs server-side, so inaccessible pages never reach JS.

assets/src/js/commands/admin-commands.js

Before:

viewCommands.forEach( ( command ) => {
    const commandUrl = addQueryArgs( command.url, command.urlArgs );
    if ( registeredCommands.some( ( cmd ) => cmd.name.endsWith( commandUrl ) ) ) {
        return;
    }
    registerCommand( command );
} );

After:

// Options page commands — one per registered SCF options page.
const optionsPageCommands = [];
if ( Array.isArray( window.scfOptionsPages ) ) {
    window.scfOptionsPages.forEach( ( optionsPage ) => {
        if ( ! optionsPage?.menu_slug ) {
            return;
        }
        optionsPageCommands.push( {
            name: 'options-page-' + optionsPage.menu_slug,
            label: optionsPage.menu_title || optionsPage.page_title,
            url: 'admin.php',
            urlArgs: { page: optionsPage.menu_slug },
            icon: page,
            keywords: [ 'options', 'settings', 'scf', optionsPage.menu_title, optionsPage.page_title ].filter( Boolean ),
        } );
    } );
}

const registerIfNotAutoRegistered = ( command ) => {
    const commandUrl = addQueryArgs( command.url, command.urlArgs );
    if ( registeredCommands.some( ( cmd ) => cmd.name.endsWith( commandUrl ) ) ) {
        return;
    }
    registerCommand( command );
};

viewCommands.forEach( registerIfNotAutoRegistered );
createCommands.forEach( registerCommand );
optionsPageCommands.forEach( registerIfNotAutoRegistered );

Why: Builds one command per page from the localized data. The dedup check is refactored into a shared helper so the new options page commands skip duplicates the same way viewCommands does on WP 6.9+.

How to test

  1. Start wp-env: npm run wp-env start.
  2. Register an options page (e.g. via the UI, or with a small plugin calling acf_update_options_page('my-page', ['menu_title' => 'My Page', 'menu_slug' => 'my-page', 'capability' => 'edit_posts'])).
  3. Open any wp-admin screen and trigger the Command Palette (Cmd/Ctrl+K).
  4. Type "My Page".
  5. Select the command.

Expected: a single command with a page icon appears, and selecting it navigates to admin.php?page=my-page.

Automated:

npm run test:e2e -- tests/e2e/command-palette.spec.ts

The new "Options page-specific commands" block asserts the command appears exactly once and navigates to the correct URL.

Testing already taken place

  • Build: npm run build clean (only pre-existing sass warnings).
  • JS unit tests: 951/951 pass.
  • PHPStan: 0 errors.
  • PHPCS: clean on changed file.
  • E2E: new describe block covers registration and navigation.

Use of AI Tools

Some drafting assistance from AI tooling (Claude). All code reviewed, tested, and adjusted by hand.

Screenshot

image

)

Register one Command Palette command per SCF options page so users can
jump straight to a page by typing its name (Cmd/Ctrl+K).

- Localize registered options pages (filtered by capability) for the
  admin command script.
- Build one command per page, deduplicated against WP 6.9+ auto-registered
  menu commands via a shared helper.
- Add E2E fixture plugin and tests covering registration and navigation.
@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, priethor.

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

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.

Add Command Palette comands to navigation to options pages

1 participant