-
-
Save tommcfarlin/e4cea9318fe9444fd4dc to your computer and use it in GitHub Desktop.
[WordPress] Using an interface to help build meta box-based functionality.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
interface Interface_Meta_Box { | |
public function add(); | |
public function display(); | |
public function save(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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. | |
*/ | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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