Code Monkey home page Code Monkey logo

wp-meta-box-factory's Introduction

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.

wp-meta-box-factory's People

Contributors

jameskerr avatar jonathantneal avatar

Watchers

 avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.