In the WordPress block editor (a.k.a. Gutenberg) the “Featured Image” is rendered at the WordPress ‘large’ image size by default. By adding a WordPress JavaScript filter to the ‘editor.PostFeaturedImage.imageSize’ hook, we can change this default rendering.
JavaScript
How to Run Code in the Browser Console
As a developer, a trick I often use is running some JavaScript directly in the browser to confirm the behavior.
How to Disable beforeunload Event Listeners
I was doing some troubleshooting and wanted to disable the “beforeunload” event listeners on a page. This is how I accomplished it with the Chrome browser.
WordPress Gutenberg Trigger Autosave
Recently, I was documenting the steps to reproduce a certain bug and the bug required the presence of an autosave (along with the “There is an autosave of this post that is more recent than the version below.” message). Rather than wait for an autosave to occur naturally, I wanted to speed things up and manually trigger one. This is the command I use to do that.
JavaScript Unix Timestamp from String in Time Zone
Given a string like “2022-01-01 13:00:00”, which represents a time in the “Europe/Paris” timezone, how can we get the corresponding Unix Timestamp in JavaScript? This problem is sufficiently complex that in my opinion leveraging a third-party library is the best solution.
WordPress Gutenberg Notice in JavaScript
You can display four types of notices in the WordPress Block Editor (a.k.a. Gutenberg) using JavaScript: error, warning, info, and success. This blog post contains an example of each.
Display Keyboard Shortcut in WordPress Gutenberg
In Gutenberg (a.k.a. the WordPress Block Editor), keyboard shortcuts are displayed differently on Apple devices and other devices, e.g. ^H on an Apple device and Ctrl+H on other devices. This is accomplished with wp.keycodes.displayShortcut.
JavaScript Or (||) Versus Nullish Coalescing Operator (??)
The “or” (||) operator and the “nullish coalescing operator” (??) can often be used in similar ways when reading a property from an object that may or may not exist. When dealing with strings you’re typically better off using “or” (||) and for numbers you’re typically better off using the “nullish coalescing operator” (??).
Promise and Async / Await
Making fetch calls with Promises vs async/await.
Join Strings with Commas and “And” in JavaScript
In English the rules we use for joining a list of items involves more work than a standard join in JavaScript. Here is JavaScript code to join an array of elements with commas and an “and”.