Meta box helper for WordPress
The metabox component uses two classes - AbstractMetaBox - Used to create the meta boxes - Helper - Used to register the meta boxes, (*1)
Lets create the pimple config for the metabox and helper:, (*2)
$pimple['helloWorldMetaBox'] = function() { return new HelloWorld(); }; $pimple['metaBoxHelper'] = function ($c) { return new MetaBoxHelper($c['objectStorage'], array( $c['pageTypeMetaBox'] )); };
Note the objectStorage is the SPLObjectStorage class., (*3)
You need to create a class that extends the AbstractMetaBox, which means implementing the methods, so lets create that now:, (*4)
class HelloWorld extends AbstractMetaBox { public function getTitle() { return "Hello world" } public function getCallback() { echo "<p>Hello world</p>"; } public function savePostCallback($postID) { update_post_meta($postID, "fp_hello_world", time()); } }
This is the bare minimum of what you must implement, the next example it the other extreme, implementing all available methods:, (*5)
class HelloWorld extends AbstractMetaBox { public function getTitle() { return "Hello world" } public function getCallback() { echo "<p>Hello world</p>"; } public function savePostCallback($postID) { update_post_meta($postID, "fp_hello_world", time()); } public function getSupportedPostTypes() { return array('page', 'post'); } public function getID() { return "helloWorldMetaBox"; } public function getContext() { return 'side'; } public function getPriority() { return 'high'; } public function getCallbackArgs() { return null; } }
Once you have setup the pimple config you are able to use the MetaBoxHelper like this:, (*6)
$helper = $pimple['metaBoxHelper']; $helper->init();, (*7)