Skip to content

Instantly share code, notes, and snippets.

@stoffl6781
Created September 23, 2025 06:54
Show Gist options
  • Select an option

  • Save stoffl6781/181791fdf153aaf2cb8a14dd8e5873e7 to your computer and use it in GitHub Desktop.

Select an option

Save stoffl6781/181791fdf153aaf2cb8a14dd8e5873e7 to your computer and use it in GitHub Desktop.

Gutenberg Link Style Dropdown

This snippet extends the Gutenberg link popover with a "Link Style" dropdown.
Editors can choose from predefined style variants (e.g. button styles), and the script will automatically add/remove the corresponding classes on the <a> element.


Features

  • Adds a <select> control to the Gutenberg link popover.
  • Toggles theme-specific classes on <a> elements.
  • Optionally wraps link text with <span class="btn-text">…</span> for button-like styling.
  • Non-destructive: only touches classes from the defined variants.
  • Runs only inside the block editor (enqueue_block_editor_assets).

Installation

  1. Copy the PHP snippet into:
    • a small custom plugin,
    • an mu-plugin, or
    • the Code Snippets plugin (PHP mode, admin only).
  2. Adjust the VARIANTS array in the JS part to match your theme’s button/link classes.
  3. Open the block editor, select a link, open the link popover → you’ll see the Link Style dropdown.

Example Variant Configuration

Define variants like this:

const VARIANTS = [
  { key: 'none',        title: '— no style —', classes: '' },
  { key: 'btn-primary', title: 'Button Primary', classes: 'btn btn-primary-custom btn-arrow' },
  { key: 'btn-link',    title: 'Button Arrow',   classes: 'btn btn-link btn-arrow' },
];

Each variant:

  • key → internal identifier
  • title → label shown in the dropdown
  • classes → classes applied to <a>

Styling

The snippet injects a small amount of CSS to integrate cleanly into the popover:

.link-style-control.components-base-control {
  padding-top: 4px;
  border-top: 1px solid rgba(0,0,0,.06);
}
.link-style-control .components-select-control__input {
  width: 100%;
}

You can modify or remove this CSS depending on your needs.


Notes

  • Safe to use: only manipulates classes defined in VARIANTS.
  • If no link is selected, the control is disabled.
  • Works seamlessly with nested or styled link content.

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