Using Modern PHP with WordPress: Part 4 – Metabox Orchestra

In the last article, we discussed Composer. That is an entire subject matter of it’s own, so I didn’t really cover everything. But I covered enough for this project.

In the Photogramappy plugin, I have registered postmeta for latitude and longitude. The idea is, someone would create a new Photograph post, upload a featured image and enter the latitude and longitude as meta on the post edit screen.

function latitude_and_longitude_metabox() {

    add_meta_box(
        'lat-long',
        __( 'Latitude and Longitude', '' ),
        'lat_long_meta_box_callback',
        'post'
    );
}
add_action( 'add_meta_boxes', 'latitude_and_longitude_metabox' );

function lat_long_meta_box_callback() {
    ?>
    <!-- Some HTML for the Metabox -->
    <?php
}

function save_lat_long( $post_id, $post ) {
    // Do some checks then save the meta
}
add_action( 'save_post', 'save_lat_long' );

This is the traditional way of registering metaboxes in WordPress but it’s so tedious, in my opinion. It also doesn’t leverage more modern approaches. For this project, I’ve opted to use the Metabox Orchestra library to handle metaboxes in an OOP way.

Each metabox needs 4 things to work. It needs a PostMetabox object which is just the creation of the metabox. Note: it could also be a TermMetabox object, if registering meta with a term. It also needs a BoxView object which renders the HTML and form fields that is in the box. Third, it needs an BoxAction, which is a class that handles the saving of the data. Finally, with those 3 objects, it is necessary to “register” the meta with WordPress. This is done with a Box object.

A note about the file structure I am using, because I’m using a number of techniques here. The Metabox.php file is an interface. All registered metaboxes (I only have one, but if I wanted to add more), would implement this interface which requires a register_meta() method. The Metabox_Object.php is an abstract class that all registered metaboxes must extend. This abstract class has the method register_meta() method that the Metabox interface requires. Almost all metabox instances would not have to declare their own, because almost all will do the exact same thing. However, a metabox could provide their own instead. Finally, the Geo_Coordinate namespace has a Geo_Coordinate.php class which extends Metabox_Object and implements MetaBox using the objects created in the Config namespace.

Object Meta
- Metabox.php
- Metabox_Object.php
- Geo_Coordinate
-- Geo_Coordinate.php
-- Config
--- Action.php
--- View.php
--- Box.php

So, in summary, Object_Meta\Geo_Coordinate\Config\View provides the form fields inside the metabox by extending the BoxView class. We handle the processing of saved data in the Object_Meta\Geo_Coordinate\Config\Action class which extends BoxAction. The Object_Meta\Geo_Coordinate\Geocoordinate class extends Object_Meta\Metabox_Object class which registers the meta by injecting a Boxes object, using the Boxes object and injecting the Box class (which extends PostMetabox).

Finally, we hook into WordPress on the metabox-orchestra.register-boxes hook. We now have metaboxes in a clean, OOP way!

See all the source code here.

In Part 5 of this series, we’ll dive a bit into Codeception and testing.

  • Category: PHP