Skip to content

chapmanu/wp-meta-box-factory

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 

Repository files navigation

Meta Box Factory is a framework for creating WordPress meta boxes that you can add to any theme or plugin.

It keeps things simple.

Example 1

$mb = new MetaBox(array(
	"title"   => "Mood Ring",
	"screens" => "page,post",
	"fields"  => array(
		"mood" => array(
			"label"   => "What mood is this?",
			"type"    => "select",
			"options" => array(
				"Excited", "Mellow", "Upset", "Comfortable"
			),
			"value"   => "Comfortable"
		)
	)
));

And helps them stay simple.

Example 2

$mb = new MetaBox(array(
	"title"   => "Mood Ring",
	"screens" => "page,post",
	"context" => "side",
	"fields"  => array(
		"summary" => array(
			"label"       => "Summarize this post",
			"type"        => "textarea",
			"rows"        => 4,
			"max"         => 144,
			"placeholder" => "Leave blank for auto-summary"
		),
		"mood" => array(
			"label"   => "What mood is this? (check all that apply)",
			"type"    => "checkbox",
			"options" => array(
				"Excited", "Mellow", "Upset", "Comfortable"
			),
			"value"   => "Comfortable"
		)
	)
));

And it supports JSON.

$mb = MetaBox::load('mood-ring.json');
{
	"title"  : "Mood Ring",
	"screens": "page,post",
	"context": "side",
	"fields" : {
		"summary": {
			"label"      : "Summarize this post",
			"type"       : "textarea",
			"rows"       : 4,
			"max"        : 144,
			"placeholder": "Leave blank for auto-summary"
		},
		"mood": {
			"label"  : "What mood is this? (check all that apply}",
			"type"   : "checkbox",
			"options": ["Excited", "Mellow", "Upset", "Comfortable"],
			"value"  : "Comfortable"
		}
	}
}

Enjoy!


Getting started

The secret to getting ahead is getting started. — Mark Twain

In your functions.php, include this framework.

require_once(TEMPLATEPATH.'/wp-meta-box-factory/meta-box-factory.php');

Now, create a new meta box.

$mb = new MetaBox();

That’s it! Of course, it won’t be visible anywhere, but that is easily amended.

$mb->add_screen('post');

Nice! Now it shows up whenever you add or edit a post. But it says “More” because you didn’t give it a title. So, give it a title.

$mb->set_title('Advanced');

You got it! You do the thing and it does the thing. Now, move it to the side.

$mb->set_context('side');

Excellent! Now, add a field, and call it “epilogue”.

$mb->add_field('epilogue', array(
	'label' => 'What happened next?'
));

You win!

Oh, but next time, write it with way less code.

$mb = new MetaBox(array(
	'screens' => 'post',
	'title'   => 'Advanced',
	'context' => 'side',
	'fields'  => array(
		'epilogue' => array(
			'label' => 'What happened next?'
		)
	)
));

How it works

I don't need to know everything, I just need to know where to find it, when I need it. — Albert Einstein

Each new meta box returns a series of chainable methods.

set_title

Sets the title of the meta box.

$mb->set_title($title);
  • title (string): The title.

add_screen

Sets the screen or screens on which to show the meta box.

$mb->add_screen($screen[, $screen]);

And, alternatively:

$mb->remove_screen($screen[, $screen]);
  • screen (string): The name of any screen, which may be post, page, dashboard, link, attachment, or some other custom screen type.

set_context

Sets the part of the page on which to show the meta box.

$mb->set_context($context);
  • context (string): The part of the page, which may be normal, advanced, or side. The default is advanced.

set_priority

Sets the priority within the context where the boxes should show.

$mb->set_priority($priority);
  • priority (string): The priority, which may be high, core, default, or low. The default is (wait for it…) default.

add_field

Adds a new field or fields to a meta box.

$mb->add_field($name, $field);
  • $name (string): the name of the field when saving to or reading from the database.
  • $field (array): the properties of the field.

Or, alternatively:

$mb->add_field($fields);
  • $fields: (array), a list of fields when saving to or reading from the database.

Loading from JSON

$mb = MetaBox::load($path[, $relative_file]);
  • $path (string): the path to the JSON file.
  • $relative_file (string): A relative path to the JSON file. Listen, unless you’re saving files relative to the wp-admin directory, you’ll probably need to add __FILE__ here.

Extending functionality

Knowing is not enough; we must apply. Willing is not enough; we must do. — Johann Wolfgang von Goethe

Need a custom input type?

$mb->add_field('magic', array(
	'type'  => 'some_custom_type'
));

Create one.

MetaBox::$create_field->some_custom_type = function ($name, $data) {
	// do stuff and return a string of HTML
};
  • $name (string): the name of the field when saving to or reading from the database.
  • $data (array): the properties of the field.

Thus far, text, textarea, password, select, checkbox, and color input types have been defined.

About

A framework for easily creating WordPress meta boxes.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages