Web . Write . WP.

Core Confidence v3.2

I started off using this post to draft the finalisation of my nthversion plugin that I’ve been working on since Core Confidence v2.8.

I opened my plugin’s test site in WordPress Studio, and drafted rough notes:

  • Use only a specific post format as the ones that are using this Block for it’s title – so do this first. Create a new post format: ‘code-ledger’ or ‘ledger’ maybe – do quick research on the best naming here. Can a post-format be created in a plugin, or does it need to be done via the theme’s functions.php file?
  • Use a filter hook to insert the Block functionality into a post
  • How to attach the existing Block feature to ‘the_title’ filter
  • Insert a playground iframe demonstrating the Block, and offer it for download.
  • Create a Github repo to host it

I used the Sync feature in Studio to pull my site from Pressable so that I can test the edits locally using the actual site, then push it all back once I was done.

Instead of recreating the wheel, I’d already been using my own tweaked version of the Format Enabler plugin by Draganescu Stefan Andrei on my site so I extended it some more and called mine, nthversion-format-enabler. I added support for a custom ‘nthversion’ format (not a real WordPress post format, but handled as a special case). That post format, ‘nthversion’, automatically inserts the nthversion block from the plugin as the first block when chosen.

I manually added ‘nthversion’ to the admin menu and dashboard widget, since it’s not part of WordPress’s predefined post formats.

The title synchronization works in two parts:

  • In the nthversion plugin’s edit.js file, the block automatically syncs its output to the post title in the database using editPost({ title: ‘Core Confidence v{versionNumber}’ }).
  • Separately, in the format-enabler plugin, a filter (block_theme_replace_title_with_nthversion()) checks if the nthversion block is the first block in a post. Whenever that is the case, the filter generates the title HTML directly from the block’s attributes, since the block’s save function returns null and doesn’t render its own output. CSS hides the title field in the editor.

I didn’t have to make many changes to the working nthversion plugin since I was just inserting the block into the format-enabler plugin. I removed CSS from the block that I previously used to distinguish block output from frontend output. I realized that unless I removed references to frontend and editor styles in the block.json file and edit.js files, the plugin would fail anytime those SCSS files are deleted.

I did have to edit the template locking for all post formats so that for nthversion post formats, editing and adding new blocks was enabled, while for the others they would remain as locked blocks upon creation.

It’s also backward compatible: if there are no existing nthversion posts, the block falls back to checking all posts for version numbers in their titles, so it works even before you create your first nthversion post (where the default ‘first post version’ is 2.8).

I learnt the command, npm run plugin-zip to output the plugin from the development folder to production-ready, as a zip file.

Because I’ve tied the nthversion-format-enabler plugin to the nthversion plugin, installation was a little tricky, as I forgot I need nthversion to be installed first, and can activate nthversion-format-enabler afterward.

I’ve created a WordPress Playground so you can test it as well. Just create a new ‘Nthversion post’, then select ‘Major’ or ‘Minor’ from the block’s settings and see the post title update automatically, depending on your selection of the version type, ‘Major’ or ‘Minor’.

My next version is likely to be bundling the post format and nthversion feature into one plugin. The dependency of the two makes me a little nervous, and the nthversion-format-enabler plugin is lightweight, with only one file. I’ll examine the best practices for this, before proceeding.

For now, they are two plugins, nthversion-format-enabler and nthversion.

I’m so happy to get to the destination (for now) that I envisioned for this nthversion plugin. I’m also proud of having done it my way – slow, steady, progress.


Discover more from jmtm

Subscribe to get the latest posts sent to your email.