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.
  1. if ( ! file_exists( $file ) )
  2. 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:
  1. include 'meta-box-3.2.2.php';
  2. 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:
  1. sample-theme
  2. - meta-box
  3. - style.css
  4. - index.php
  5. - functions.php
  6. - other files
You also can put the meta-box inside a sub-folder, such as inc or lib:
  1. sample-theme
  2. - inc/meta-box
  3. - inc/custom
  4. - style.css
  5. - index.php
  6. - other files

2. Redefine meta box script path and URL

Put the following code into your functions.php file:
  1. // Re-define meta box path and URL
  2. define( 'RWMB_URL', trailingslashit( get_stylesheet_directory_uri() . '/meta-box' ) );
  3. define( 'RWMB_DIR', trailingslashit( STYLESHEETPATH . '/meta-box' ) );
  4. // Include the meta box script
  5. require_once RWMB_DIR . 'meta-box.php';
  6. // Include the meta box definition (the file where you define meta boxes, see `demo/demo.php`)
  7. include 'config-meta-boxes.php';
If you put meta box script inside a sub-folder (for ex. inc), then the code will look like:
  1. // Re-define meta box path and URL
  2. define( 'RWMB_URL', trailingslashit( get_stylesheet_directory_uri() . '/inc/meta-box' ) );
  3. define( 'RWMB_DIR', trailingslashit( STYLESHEETPATH . '/inc/meta-box' ) );
  4. // Include the meta box script
  5. require_once RWMB_DIR . 'meta-box.php';
  6. // Include the meta box definition (the file where you define meta boxes, see `demo/demo.php`)
  7. include 'config-meta-boxes.php';
Note: If you want to use your theme as a parent theme (i.e. not a child theme), please changeSTYLESHEETPATH above to TEMPLATEPATH.
This is the sample TwentyTwelve theme integrated with the Meta box plugin. Please download and play with it.



(Chú Thích: Copy thư muc plugin meta-box + file meta.php vào template sao đó vào functions.php khai báo)

Nhận xét

Bài đăng phổ biến từ blog này

Tối Ưu Nginx Và PHP-FPM cho các website có lượng truy cập lớn

Code đặt mật khẩu cho file PHP

Hướng dẫn cài đặt Memcached trên CentOS (tăng tốcc web)