Dublin Library

The Publishing project

Plugin Topics: JSON Block Configuration

Starting in WordPress 5.8, there is a JSON metadata file we can use to register blocks. The block.json file is the block-level equivalent to theme.json and is now the preferred way to register blocks. The following example contains a block.json definition for a fictional my-plugin/notice block. json { "apiVersion": 2, "name": "my-plugin/notice", "title": "Notice", "category": "text", "parent": "core/group" , "icon": "star", "description": "Shows warning, error or success notices…", "keywords": "alert", "message" , "version": "1.0.3", "textdomain": "my-plugin", "attributes": { "message": { "type": "string", "source": "html", "selector": ".message" } }, "providesContext": { "my-plugin/message": "message" }, "usesContext": "groupId" , "supports": { "align": true...

Continue reading →

Plugin Topics: Meta boxes in the Block Editor

Meta boxes work in Gutenberg but they may not do so for a while or be the best solution for your needs. During the full migration to the Block Editor, WordPress Provides two compatibility flagshttps://make.wordpress.org/core/2018/11/07/meta-box-compatibility-flags/. Most of the time the compatibility setup works out of the box. However, you must test the meta boxes on Gutenberg to make sure the code will still work and that you won't need to write new blocks to handle the meta box data. You also need to modify the addmetabox function to look like the example that follows: php true, 'backcompatmetabox' => false, ;...

Continue reading →

Color.js A CSS Color Tool in Javascript

CSS Color Module Level 4https://drafts.csswg.org/css-color-4/ and level 5https://drafts.csswg.org/css-color-5/ introduce a lot of new color features but unless you're really into the guts of working with colors on computers you probably wouldn't know how to use them. There is also an issue of browser uneven support for colors on the web, which is a large problem for developers. When the graphical web first came online it was in low-resolution monitors, 640 x 480 was the norm and RGB was the only color space that we had to worry about. As technology improved, displays got bigger and grew beyond RGB and what...

Continue reading →

Plugin Topics: Thinking about privacy

Privacy in the context of a web application like WordPress goes beyond legal requirements. It's also answering the following questions: What information we collect Why we collect it How we use it How we protect it Taken from ISO 29100/Privacy Framework standardhttps://www.iso.org/standard/45123.html, the following checklist provides a good starting point for thinking about privacy in general but more applicable in the context of a WordPress plugin that collects data from users and uses that data to create a service or site: Consent and choice: giving users and site visitors choices and options over the uses of their data, and requiring...

Continue reading →

Plugin Topics: Adding Plugin Admin Pages

If we want to allow users to configure our plugin, we can add menus to the admin pages either as a top-level menu or a submenu of an existing menu. When used with the settings and options APIs we get control of how users configure the plugin from the admin area, assuming that they have permission to do so. Top Level Menus Top-Level Menushttps://developer.wordpress.org/plugins/administration-menus/top-level-menus/ are what people see when they first view the admin area. This means that our admin page has more visibility but, depending on the plugin, it may be better to use a submenu The first step...

Continue reading →

Plugin Topics: Meta Boxes and Cron

The next few items Meta Boxes, and Cron are nice to have, require a lot more work, and are not necessary for the majority of plugin projects. I include them here because I'm interested in what they do and can think of a few projects that would benefit from one or more of them. Custom Meta Boxes Custom Meta Boxeshttps://developer.wordpress.org/plugins/metadata/custom-meta-boxes/ allows you to create custom sections of the editor screen where users can enter custom data and you can then sanitize it and save it to the database. Because they allow custom data we need to follow all the security...

Continue reading →

Additional considerations for OOP plugin development

Rather than writing plugins or any PHP code procedurally, we can leverage Object-Oriented Programming OOP to write our plugins. OOP is easier to maintain and extend as the needs of the plugin increase. The code below is a good starting point for class-based plugins. It doesn't have the full plugin comment but the code should still work even if it does nothing yet. php setupactions; } / Setting up Hooks / public function setupactions { //Main plugin hooks registeractivationhook FILE, array 'AwesomePlugin', 'activate' ; registerdeactivationhook FILE, array 'AwesomePlugin', 'deactivate' ; } / Activate callback / public static function activate {...

Continue reading →

Notes On Building A WordPress Plugin

Anyone can build a plugin but it takes a lot of work to get the plugin done right and ready for sharing or WordPress review and approval. I realized the extra work involved when I was looking at the result of creating a plugin scaffold using the WordPress Plugin Boilerplate Generatorhttps://wppb.me/. The idea of the generator, just like its downloadble siblinghttps://wppb.io/, is to provide a good starting point for creating a plugin. There are other generators available, some of which may be better suited to smaller, single-purpose tasks like what I have in mind: WordPress Plugin Boilerplatehttps://github.com/DevinVinson/WordPress-Plugin-Boilerplate : A foundation...

Continue reading →

Add inline scripts in WordPress

When working with WordPress, you may need to insert an inline script into a page or a template. This post will discuss two ways to do it Method 1: wp\add\inline\script The first method, wpaddinlinescript works with enqueued scripts and uses the same handle that we enqueued the script with to add the inline script to the page. With this method, we can ensure that the enqueued script will be added to the page before the inline script. php tag. You have to write the full script, including opening and closing tags but it gives you more flexibility in how you...

Continue reading →

Live Core Web Vitals Measurements In WordPress

Building the Core Web Vitals measurement into WordPress is a bit more involved as it depends on whether you've analytics into your site and how you've done it. This function depends on Google's Sitekit pluginhttps://sitekit.withgoogle.com/ and relies on values set there. We can run a function that checks if the gtag script has already been enqueued on your site: If it's hasn't been enqueued, then you add it and configure it If it's already been enqueued, then do nothing, as it's ready to use If you use a different tool to set up your Google Analytics or use a different...

Continue reading →