Saturday, January 18, 2025
HomeEveryday WordPressCustomize the WordPress Command Palette with your plugin

Customize the WordPress Command Palette with your plugin


The release of WordPress 6.3 introduced the Command Palette, a feature that offers quick access to actions frequently used by those who edit content or themes within the CMS’s admin interface.

The menu-like Command Palette allows users to filter available tasks using a simple search interface and select options to help navigate the editor’s UI, toggle preferences, transform styles, manipulate blocks, and even begin creating new posts and pages.

Also available is a JavaScript-enabled API that allows developers to add functionality to the Command Palette. For example, the creator of a WordPress plugin that generates Web forms might add a Command Palette entry that whisks users off to a page that shows the results of form submissions.

The developer of a plugin that uses many shortcodes might link an entry in the Command Palette to a pop-up “cheat sheet” to remind users how to use those codes.

The possibilities are endless, and we’re giving you a taste of how the API works to inspire you to use the Command Palette in your next WordPress plugin project.

WordPress Command Palette basics

You launch the Comand Palette by using the keyboard shortcut Cmd + k (Mac) or Ctl + k (Windows and Linux) or clicking on the Title field at the top of the Post Editor or Site Editor:

The Command Palette is activated within the Post Editor.

The top of the Palette includes a search field that filters entries as you type. You can select entries using a mouse or using the keyboard alone.

A partial list of commands available out of the box in the Palette includes:

  • Edit Template (when editing a page)
  • Back to page (after editing its template)
  • Reset template
  • Reset template part
  • Reset styles to default
  • Delete template
  • Delete template part
  • Toggle settings sidebar
  • Toggle block inspector
  • Toggle spotlight mode
  • Toggle distraction free
  • Toggle top toolbar
  • Open code editor
  • Exit code editor
  • Toggle list view
  • Toggle fullscreen mode
  • Editor preferences
  • Keyboard shortcuts
  • Show/hide block breadcrumbs
  • Customize CSS
  • Style revisions
  • Open styles
  • Reset styles
  • View site
  • View templates
  • View template parts
  • Open navigation menus
  • Rename Pattern
  • Duplicate Pattern
  • Manage all custom patterns

And, of course, developers can add their own to enhance their WordPress applications. Let’s jump into the process!

What you’ll need to get started

The Comand Palette API is supported by JavaScript packages you will add to your projects using npm, the Node Package Manager. You’ll need an installation of WordPress (local or remote) that you can access via the terminal to execute commands on the command line.

To get things rolling, we created a WordPress plugin that will be home to the code that modifies the Command Palette. The plugin does little more than create a custom post type that we call Products. (Everything you need to know to get this far with a rudimentary plugin is available in our guide to creating custom post types.)

When our Product Pages plugin is activated, we gain a Dashboard menu entry for creating and browsing Product posts:

A screenshot of the WordPress Plugins page and an arrow pointing to the entry for a custom Products content type in the Admin menu.
Our Product Pages plugin provides a new post content type called Products.

Our plugin does not have any unique assistance from the WordPress Command Palette. For example, the default functionality of the Command Palette provides shortcuts to add new WordPress posts and pages:

Screenshot of the WordPress Command Palette showing the entries for creating posts and pages.
The shortcuts to add new posts or pages are frequently used in the Command Palette.

However, the Command Palette knows nothing about creating our Product pages. We are adding that functionality below.

Adding a custom command to the Command Palette

Right now, our entire Product Pages plugin consists of a single PHP file that we have named products.php and placed in wp-content/plugins/products. Other than enable the Products post type, it doesn’t do anything yet. We’ll come back to this file after we’ve set up the JavaScript-powered API for the Command Palette.



Source link

RELATED ARTICLES
Continue to the category

LEAVE A REPLY

Please enter your comment!
Please enter your name here


Most Popular

Recent Comments