• Skip to primary navigation
  • Skip to main content
Sal Ferrarello
  • About Sal Ferrarello
  • Speaking
  • Connect
    Mastodon GitHub Twitter (deprecated)
You are here: Home / Dev Tips / How to Close and Re-Open WordPress Gutenberg Sidebar

How to Close and Re-Open WordPress Gutenberg Sidebar

Last updated on June 30, 2023 by Sal Ferrarello

The sidebar gets a lot of use in the WordPress Block Editor, a.k.a. Gutenberg, but I found myself in a situation where I needed to programmatically close the sidebar and then re-open it later. This is how I did it.

// Save the name of the currently open sidebar to generalSidebarName.
generalSidebarName = wp.data.select('core/edit-post')
  .getActiveGeneralSidebarName();

// Close the sidebar.
await wp.data.dispatch('core/edit-post').closeGeneralSidebar();

At some point later to the sidebar name we stored in generalSidebarName.

// Re-open the sidebar (to the same sidebar that had been open).
wp.data.dispatch('core/edit-post').openGeneralSidebar(generalSidebarName);
Sal Ferrarello
Sal Ferrarello (@salcode)
Sal is a PHP developer with a focus on the WordPress platform. He is a conference speaker with a background including Piano Player, Radio DJ, Magician/Juggler, Beach Photographer, and High School Math Teacher. Sal can be found professionally at WebDevStudios, where he works as a senior backend engineer.

Filed Under: Dev Tips, Programming, Solution Tagged With: Gutenberg, JavaScript, WordPress

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2023 · Bootstrap4 Genesis on Genesis Framework · WordPress · Log in