dev-master
9999999-dev https://github.com/mrbobbybryant/metabox-controlA Library which handles conditionally showing and hide metaboxes on a per template bases.
GPL-2.0+
A Library which handles conditionally showing and hide metaboxes on a per template bases.
Table of Contents, (*1)
Metabox Control is a small Javascript API that allows you to take back control of your Page edit screen. One metabox at a time. Metabox Control allows you to control when metaboxes are rendered to the screen., (*2)
Over time, it never fails. You create 2 or 3 custom page templates. You add 2 or 3 metaboxes to provide the end user an outstanding WordPress experience. And then BAM. Your perfectly laid plans become a cluttered mess., (*3)
Metabox Control allows you to specify which metaboxes belong to which page template. And thats all you have to do. Wait until use see the Usage section. It is super minimal., (*4)
From there Metabox Control will handle the showing and hiding of your metaboxes. Which is based on the currently selected template from the page template dropdown. So lets look at some code., (*5)
Clone or download this repo to your WordPress project's plugin directory., (*6)
$ git clone https://github.com/mrbobbybryant/metabox-control.git
Once you have the code downloaded, simply activate the plugin just like normal., (*7)
Metabox Control provides both a Javscript and a PHP API for registering Templates and their associted metaboxes., (*8)
This method is used to register a page template and it's associated metaboxes., (*9)
templateName
- (string) - required - This is the php file name. For example, page-template-one.php
metabox-ids
- (array) - required - An array of all the metabox ids you wish to show and hide, depending on this page template. These IDs are the same ones used when registering a metabox. When registering a metabox, this will be the first parameter passed into add_meta_box()
.metaboxControl.addTemplate( templateName, array(metabox-ids));
This method will remove the given template from Metabox Control., (*10)
templateName
- (string) - required - This is the php file name. For example, page-template-one.php
. This should correlate to a previously registered template., (*11)
Returns - New array -or- Error., (*12)
metaboxControl.removeTemplate( templateName );
This method does not except any parameters. It allows you to query Metabox Control and get a list of all templates that it is tracking. This can be useful for troubleshooting., (*13)
registeredTemplates
array which is encapsulated within Metabox Control.metaboxControl.getTemplates();
This method does not except any parameters. It is simply a helper function for quickly see which WordPress page template is currently selected., (*14)
metaboxControl.currentTemplate();;
The PHP code has been namespaced. The Public API uses the namespace metabox_control\API;
., (*15)
This method is used to register a page template and it's associated metaboxes. It will also check if the request template has already been registered. If so it will call `update_metabox_templates``` internally., (*16)
template
- (string) - required - This is the php file name. For example, page-template-one.php
metaboxes
- (array) - required - An array of all the metabox ids you wish to show and hide, depending on this page template. These IDs are the same ones used when registering a metabox. When registering a metabox, this will be the first parameter passed into add_meta_box()
.\metabox_control\API\add_metabox_template( 'page-two.php', array('mb_two') );
This method is used to remove a previously registers template, (*17)
template
- (string) - required - This is the php file name. For example, page-template-one.php
\metabox_control\API\remove_metabox_template( 'page-two.php' );
This method is used to update a previously registers template., (*18)
template
- (string) - required - This is the php file name. For example, page-template-one.php
metaboxes
- (array) - required - This is an array of metabox id.- (bool) - **optional** - This argument is used internally by
add_metabox_template()``` when it determines
a template has already been entered.\metabox_control\API\remove_metabox_template( 'page-two.php', array('mb_two') );
In the example below I will be simiulating the adding of two page templates in Javascript ( page-one.php and page-two.php ), as well as a few metabox( mb_one, mb_three and mb_two ). Again these will depend on your implementation, and is simply an example., (*19)
metaboxControl.addTemplate( 'page-one.php', ['mb_one', 'mb_three']); metaboxControl.addTemplate( 'page-two.php', ['mb_two']);
That's it! Now when you go to Create or Edit a WordPress Page, those metaboxes will only be visible when you have selected the associated page template., (*20)
A Library which handles conditionally showing and hide metaboxes on a per template bases.
GPL-2.0+