Include Meta Box Plugin In Themes

Though Meta Box actually is a plugin, but you can still use it in a theme without conflicts.

IMPORTANT NOTE:

Please, always test if a file exists, before including it.

if ( ! file_exists( $file ) )
    wp_die( "The file at location {$file} doesn't exist. Check your pathes!" );

Include Meta Box Script version < 4.0

In previous versions of Meta Box Script (< 4.0), you can simply put the script files into theme folder and include 2 files:

include 'meta-box-3.2.2.php';
include 'meta-box-usage.php';

Include Meta Box Script version >= 4.0

Since version 4.0, the script has wrapped into a plugin, and thus the code above might not work properly. To include the Meta Box Script in a theme, please follow the guide below:

1. Download the script and put it in your theme folder

For example, if your theme name is sample-theme, then your folder structure after copying meta box script will look like:

sample-theme
- meta-box
- style.css
- index.php
- functions.php
- other files

You also can put the meta-box inside a sub-folder, such as inc or lib:

sample-theme
- inc/meta-box
- inc/custom
- style.css
- index.php
- other files

2. Redefine meta box script path and URL

Put the following code into your functions.php file:

// Re-define meta box path and URL
define( 'RWMB_URL', trailingslashit( get_stylesheet_directory_uri() . '/meta-box' ) );
define( 'RWMB_DIR', trailingslashit( STYLESHEETPATH . '/meta-box' ) );

// Include the meta box script
require_once RWMB_DIR . 'meta-box.php';

// Include the meta box definition (the file where you define meta boxes, see `demo/demo.php`)
include 'config-meta-boxes.php';

If you put meta box script inside a sub-folder (for ex. inc), then the code will look like:

// Re-define meta box path and URL
define( 'RWMB_URL', trailingslashit( get_stylesheet_directory_uri() . '/inc/meta-box' ) );
define( 'RWMB_DIR', trailingslashit( STYLESHEETPATH . '/inc/meta-box' ) );

// Include the meta box script
require_once RWMB_DIR . 'meta-box.php';

// Include the meta box definition (the file where you define meta boxes, see `demo/demo.php`)
include 'config-meta-boxes.php';

Note: If you want to use your theme as a parent theme (i.e. not a child theme), please change STYLESHEETPATH above to TEMPLATEPATH.

This is the sample TwentyTwelve theme integrated with the Meta box plugin. Please download and play with it.

See more tutorials