0

I have a question, and I am really sorry when it has been already asked (and answered) - but I am researching for hours now and I can't find the answer. Although...somehow I can't believe it, that nobody needs this, so maybe I am completely wrong.

But let me tell you my aim:

I am creating a website for grammar - so I need to "highlight" many words inline. I would like to keep these designs consistent, so the best would be to define styles, like "Word in foreign language", "Word in my language", "Suffix", etc... And then just use them in the paragraph, like the existing ones:

Image

How does that work? Creating the CSS is no problem, but how can I add it to the dropdown? Or are there any other options, that I am not thinking of?

Thanks a lot for helping! This (and the soon-coming patterns with overrides...) would really help me...

1 Answer 1

1

To add custom options to that dropdown, you'd want to look at utilizing the Formatting API. Below are the main points quoted from the Block Editor Handbook step-by-step guide:

Step 1: Register a new format

The first step is to register the new format, add src/index.js with the following:

import { registerFormatType } from '@wordpress/rich-text';

registerFormatType( 'my-custom-format/sample-output', {
    title: 'Sample output',
    tagName: 'samp',
    className: null,
} );

[…]

Step 2: Add a button to the toolbar

With the format available, the next step is to add a button to the UI by registering a component for the edit property.

Using the RichTextToolbarButton component, update src/index.js:

import { RichTextToolbarButton } from '@wordpress/block-editor';

const MyCustomButton = ( props ) => {
    return (
        <RichTextToolbarButton
            icon="editor-code"
            title="Sample output"
            onClick={ () => {
                console.log( 'toggle format' );
            } }
        />
    );
};

registerFormatType( 'my-custom-format/sample-output', {
    // …
    edit: MyCustomButton,
} );

[…]

Step 3: Apply a format when clicked

Next is to update the button to apply a format when clicked.

import { registerFormatType, toggleFormat } from '@wordpress/rich-text';
import { RichTextToolbarButton } from '@wordpress/block-editor';

const MyCustomButton = ( { isActive, onChange, value } ) => {
    return (
        <RichTextToolbarButton
            icon="editor-code"
            title="Sample output"
            onClick={ () => {
                onChange(
                    toggleFormat( value, {
                        type: 'my-custom-format/sample-output',
                    } )
                );
            } }
            isActive={ isActive }
        />
    );
};

Further reading:

4
  • Thanks, your info and point to the docs helped! I created my first "new" (as in: with React) wordpress-plugin! One small question: I started now off with npx @wordpress/create-block multi-columns, but I don't need all that "new block" logic. How do I create a project where I just want to add the format?
    – webwurm
    Commented May 4 at 20:38
  • Have a JavaScript file with the steps above that gets compiled by wp-scripts package. Enqueue the compiled file into the block editor via the enqueue_block_editor_assets hook.
    – Wongjn
    Commented May 4 at 20:45
  • Thanks again, I sadly can't get it to work - when I create a new plugin with wp-scripts, create the files and folders, add the infos and scripts to the package.json - I still don't see a plugin appear. But I don't want to bother you further. Do you maybe know a tutorial for creating a plugin with wp-scripts? Everything I find is either with the create-block or is depricated. I tried around with the info I found in the docs too, but sadly without luck.
    – webwurm
    Commented May 6 at 10:40
  • No, I don't know a tutorial for creating a plugin with wp-scripts.
    – Wongjn
    Commented May 6 at 11:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.