Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
[WordPress] Using an interface to help build meta box-based functionality.
interface Interface_Meta_Box {
public function add();
public function display();
public function save();
class Example_Meta_Box implements Interface_Meta_Box {
public function init() {
add_action( 'add_meta_boxes', array( $this, 'add' ) );
public function add() {
'Example Meta Box',
array( $this, 'display' ),
public function display() {
echo "This is an example meta box.";
public function save() {
/* This is where the save functionality goes.
* It should check to make sure the current user has permission
* to save, and then should update whatever information - such as post meta -
* that's relevant to the current post and the data presented in the meta box,
* if any.
* Plugin Name: Meta Box Example
* Description: Demonstrates the use of Meta Boxes, interfaces, and more in WordPress.
* Version: 1.0.0
* Author: Tom McFarlin
* Author URI:
* License: GPL-2.0+
* License URI:
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
* The interface that defines the functions for the meta box.
include_once( 'interface-meta-box.php' );
* The class that implements the interface and defines the meta box.
include_once( 'class-example-meta-box.php' );
* Instantiates the main class and initializes the plugin.
function mbe_start() {
$plugin = new Example_Meta_Box();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.