Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[WordPress] Using an interface to help build meta box-based functionality.
<?php
interface Interface_Meta_Box {
public function add();
public function display();
public function save();
}
<?php
class Example_Meta_Box implements Interface_Meta_Box {
public function init() {
add_action( 'add_meta_boxes', array( $this, 'add' ) );
}
public function add() {
add_meta_box(
'example_meta_box',
'Example Meta Box',
array( $this, 'display' ),
'post'
);
}
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.
*/
}
}
<?php
/**
* 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: https://tommcfarlin.com/
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
*/
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
/**
* 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();
$plugin->init();
}
mbe_start();
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.