Metaboxes in WordPress
What are metaboxes??
In order not to be silent, I will say, what WordPress there are standard metaboxes and you often use them. Most often you see metaboxes “Discussion” (Discussion) and Custom Fields (Arbitrary fields). Those, who has been working with WordPress for some time, know that you can disable and connect standard metaboxes in the item Screen Options (Screen settings).
When you develop sites, you use plugins and many do Metabox API. This technology is often used in SEO plugins to add header input fields, description and keywords. By the way, it was tested experimentally, the meta tag of keywords on the pages can not be added to the issue at all, I did not notice, to influence.
An example of a metabox in plugins.
Before creating your metaboxes, it is necessary to learn to remove the extra ones.
remove_meta_box() – removal of the metabox.
1 |
remove_meta_box( $id, $page, $context ); |
$page (line) - type of posts, on the creation page / editing which you need to remove the metabox, For example, post, page.
$context (line) - the location of the metabox, for example normal, advanced abo side.
For the function to work, it must be placed in functions.php. For example, let's remove the layer with the author's name authordiv.
1 2 3 4 5 |
function remove_author_div() { remove_meta_box( 'authordiv' , 'post' , 'side' ); // удалим метабокс с рубриками } add_action( 'admin_menu' , 'remove_author_div' ); |
If you paste this code, then in Screen Optn (screen settings) there will be no author metabox selection. Also, when editing an article, you will not see this metabox.
Let's consider what other standard metaboxes can be used to develop plugins.
Metabox identifiers could also be found in the code, but there are a number of standard ones, which I think, it is important to list. I will list the features of standard metaboxes.
comment status div – discussion settings.
commentsdiv – displays comments on the current position.
slugdiv – record shortcut.
audit div - editors.
authordiv - the author of the record.
postcustom - addition / editing of arbitrary fields.
postexcerpt - quote.
trackbacksdiv – backlinks.
categorydiv – metabox list of WordPress headings.
tagsdiv-post_tag - labels.
postimagediv – record thumbnail.
pageparentdiv – metabox with selection of parent page and page template.
submitdiv - a block with a "publish" button.
Adding metaboxes with register_post_type()
Adding register_post_type metaboxes()
I think you have heard about content types in WordPress. Standard types are pages, posts and those, which you can create yourself. So here it is, this function is used to create a material type. In it, you can immediately determine which metaboxes the created types will support.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
function codex_custom_init() { $labels = array( 'name' => _x('Books', 'post type general name', 'your_text_domain'), 'singular_name' => _x('Book', 'post type singular name', 'your_text_domain'), 'add_new' => _x('Add New', 'book', 'your_text_domain'), 'add_new_item' => __('Add New Book', 'your_text_domain'), 'edit_item' => __('Edit Book', 'your_text_domain'), 'new_item' => __('New Book', 'your_text_domain'), 'all_items' => __('All Books', 'your_text_domain'), 'view_item' => __('View Book', 'your_text_domain'), 'search_items' => __('Search Books', 'your_text_domain'), 'not_found' => __('No books found', 'your_text_domain'), 'not_found_in_trash' => __('No books found in Trash', 'your_text_domain'), 'parent_item_colon' => '', 'menu_name' => __('Books', 'your_text_domain') ); $args = array( 'labels' => $labels, 'public' => true, 'publicly_queryable' => true, 'show_ui' => true, 'show_in_menu' => true, 'query_var' => true, 'rewrite' => array( 'slug' => _x( 'book', 'URL slug', 'your_text_domain' ) ), 'capability_type' => 'post', 'has_archive' => true, 'hierarchical' => false, 'menu_position' => null, 'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' ) ); register_post_type('book', $args); } add_action( 'init', 'codex_custom_init' ); |
The line Support shows, which metaboxes the new data type should use.
1 |
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' ) |
You can also add a category and tags for a new data type using an array taxonomies.
1 |
'taxonomies' => array('category', 'post_tag'), |
Full code view for our new data type:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
function codex_custom_init() { $labels = array( 'name' => _x('Books', 'post type general name', 'your_text_domain'), 'singular_name' => _x('Book', 'post type singular name', 'your_text_domain'), 'add_new' => _x('Add New', 'book', 'your_text_domain'), 'add_new_item' => __('Add New Book', 'your_text_domain'), 'edit_item' => __('Edit Book', 'your_text_domain'), 'new_item' => __('New Book', 'your_text_domain'), 'all_items' => __('All Books', 'your_text_domain'), 'view_item' => __('View Book', 'your_text_domain'), 'search_items' => __('Search Books', 'your_text_domain'), 'not_found' => __('No books found', 'your_text_domain'), 'not_found_in_trash' => __('No books found in Trash', 'your_text_domain'), 'parent_item_colon' => '', 'menu_name' => __('Books', 'your_text_domain') ); $args = array( 'labels' => $labels, 'public' => true, 'publicly_queryable' => true, 'show_ui' => true, 'show_in_menu' => true, 'query_var' => true, 'rewrite' => array( 'slug' => _x( 'book', 'URL slug', 'your_text_domain' ) ), 'capability_type' => 'post', 'has_archive' => true, 'hierarchical' => false, 'taxonomies' => array('category', 'post_tag'), 'menu_position' => null, 'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' ), ); register_post_type('book', $args); } add_action( 'init', 'codex_custom_init' ); |
Metaboxes can also be added using the function register_taxonomy_for_object_type()
The function assigns a taxonomy to the desired record type, a metabox is also added:
1 2 3 4 |
$taxonomy = 'post_tag'; // таксономія, у цьому випадку мітки $post_type = 'game'; // тип посту register_taxonomy_for_object_type($taxonomy, $post_type); |
This function can be used, if the taxonomy does not need to be defined first.
It is also possible to add supported metaboxes not immediately during the register_post_type function(), and during further processing of the code. To add metabox support, use the add_post_type_support function() instead of the supports array.
1 2 3 4 5 6 7 8 |
function add_some_std_metaboxes() { add_post_type_support('game', 'excerpt'); // добавляем метабокс с цитатой для типа записей game add_post_type_support('movie', array('comments', 'thumbnail', 'excerpt')); // метабокс с комментами, миниатюрой и цитатой для типа записей movie } add_action('init', 'add_some_std_metaboxes'); |
Add your metaboxes add_meta_box()
You can add absolutely any custom metabox, does not matter, whether it will be fields for specifying meta tags or an image uploader to the gallery.
1 |
add_meta_box($id, $title, $callback, $post_type, $context, $priority, $args); |
$id (line) (mandatory) – HTML-attribute id for the div block of the future metabox,
$title (line) (mandatory) - header,
$callback (function) (mandatory) - a function that will fill the metabox, you need to specify the function name as a string,
$post_type (line) (mandatory) – type of records,
$context (line) - in which part of the page to insert the metabox (normal, side, advanced), Default - advanced,
$priority (line) - priority, the higher it is, the closer to the top of the page the metabox will be, (high, core, default or low), by default - default,
$args (array) - arguments for callback functions.
Unlike the previous methods, adding your metabox will take place in several stages, consider an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
/* * Этап 1. Додавання */ function tr_meta_boxes() { add_meta_box('truediv','Настройки','tr_echo_box','book','normal','high'); } add_action( 'admin_menu', 'tr_meta_boxes' ); /* також можна використовувати й інші хуки: add_action( 'add_meta_boxes', 'tr_meta_boxes' ); Якщо версія WordPress нижче 3.0, від add_action( 'admin_init', 'tr_meta_boxes', 1 ); */ /* * Этап 2. Заповнення */ function tr_echo_box($post) { wp_nonce_field( basename( __FILE__ ), 'seo_metabox_nonce' ); $title = get_post_meta($post->ID, '_seo_title',true); $noindex = get_post_meta($post->ID, '_seo_noindex',true); ?> <label>Заголовок <input name="seotitle" type="text" value="<?php echo $title ?>" /> </label> <label> <input checked="checked" name="noindex" type="checkbox" /> /> Приховати запис від пошукових систем? </label> <!--?php } /* * Этап 3. Сохранение */ function tr_save_box( $post_id ) { // проверяем, пришёл ли запрос со страницы с метабоксом if ( !isset( $_POST['seo_metabox_nonce'] ) || !wp_verify_nonce( $_POST['seo_metabox_nonce'], basename( __FILE__ ) ) ) return $post_id; // проверяем, является ли запрос автосохранением if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) return $post_id; // проверяем, права пользователя, может ли он редактировать записи if ( !current_user_can( 'edit_post', $post_id ) ) return $post_id; // теперь также проверим тип записи $post = get_post($post_id); if ($post--->post_type == 'book') { // вкажіть власний update_post_meta($post_id,'_seo_title',esc_attr($_POST['seotitle'])); update_post_meta($post_id,'_seo_noindex', $_POST['noindex']); } return $post_id; } add_action('save_post','tr_save_box'); ?> |
If, after pasting the code, you try to create a record in our new Books data type, then when created you will see a new metabox with our content.
Adding metaboxes using a class
The author from whom I peeked a part of the article promised to teach more lessons about creating widgets, I think I will follow him and look for him myself, the topic is really interesting.
Class description:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
<!--?php if ( !class_exists('trueMetaBoxes') ){ class trueMetaBoxes { /* * параметры по умолчанию, например префикс для полей в таблице MySQL */ var $prefix = '_true_'; /* * массив с произвольными полями * содержание массива довольно размыто, * то есть никто не заставляет вас использовать те же самые ключи */ var $options = array( array( "name" =-->"title", // атрибути id та name поля форми "title" => "Заголовок", // назва поля "description" => "Содержимое тега title", // опис "type" => "text", // тип поля "cap" => "edit_posts", // необхідні права користувача "val" => "" // значення за замовчуванням ), array( "name" => "noindex", "title" => "Дозволити канонічні URL?", "type" => "checkbox", "cap" => "edit_posts" ), array( "name" => "robots", "title" => "Вміст мета-тегу robots", "type" => "select", "cap" => "edit_posts", "val" => array( 'index' => 'index,follow', 'noindex' => 'noindex,follow', 'nofollow' => 'nofollow, index', 'noindexnofollow' => 'noindex, nofollow' ) ) ); /* * конструктор класу, під час створення об'єкта сюди передаємо параметри $id и $title * вони знадобляться у функції add_meta_box() */ function __construct($id, $title) { $this->id = $id; $this->title = $title; add_action( 'admin_menu', array( &$this, 'create_true_box' ) ); add_action( 'save_post', array( &$this, 'save_true_box' ), 1, 2 ); } /* * функція додавання метабоксу * як бачите, в конструкторі я передав лише найважливіші аргументи * але так само можна легко налаштовувати і інші */ function create_true_box() { add_meta_box( $this->id, $this->title, array( &$this, 'display_true_box' ), 'post', 'normal'); } /* * функція, що генерує вміст метабоксу */ function display_true_box(){ global $post; ?> |
options as $option ) { if (current_user_can( $option[‘cap’], $post->ID )) { ?>
prefix . $option[‘name’] . ‘” id=”‘ . $this->prefix . $option[‘name’] . ‘”‘; if ( get_post_meta( $post->ID, $this->prefix . $option[‘name’], true ) == “on” ) echo ‘ checked=”checked”‘; echo ‘” style=”width: auto;” />’; echo ‘‘; break; } case “select”: { echo ‘
‘ . $option[ ‘description’ ] . ‘
‘; break; } default: { // default type="text" echo '
‘ . $option[ ‘description’ ] . ‘
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<!--?php } /* * функция сохранения произвольных полей */ function save_true_box($post_id, $post){ if ( !wp_verify_nonce( $_POST[ 'seodiv_wpnonce' ], 'seo-div' ) ) return; if ( !current_user_can( 'edit_post', $post_id ) ) return; if ( $post--->post_type != 'page' && $post->post_type != 'post' ) return; foreach ( $this->options as $option ) { if ( current_user_can( $option['cap'], $post_id ) ) { if ( isset( $_POST[ $this->prefix . $option['name'] ] ) && trim( $_POST[ $this->prefix . $option['name'] ] ) ) { // як бачите, вся інформація зберігається в метаданих посту update_post_meta( $post_id, $this->prefix . $option[ 'name' ], $_POST[ $this->prefix . $option['name'] ] ); } else { delete_post_meta( $post_id, $this->prefix . $option[ 'name' ] ); } } } } } } |
1 2 3 4 5 |
if (class_exists('trueMetaBoxes')) { $id = 'seodiv'; // id метабокса $title = 'Доп. Налаштування'; // заголовок $obj = new trueMetaBoxes($id, $title); } |
It is enough to describe the class in detail once and then create as many objects as you like (these will be metaboxes), just change the settings, that are transferred to him, including the $options array, you won't have to write a bunch of HTML code, everything will be inserted according to the template. Variables, which are not used by the constructor, can be changed after the object is created, For example:
1 2 3 |
// тобто. кожному об'єкту можна присвоїти свої власні $prefix та $options $obj->prefix = '_seo_'; $obj->options[0]['title'] = 'Типу новий лейбл першого поля у метабоксі'; |
Reference to metadata (arbitrary fields) post / pages
1 |
get_post_meta($post_id, $key, $single); |
$post_id (whole) (mandatory) – post or page ID.
$key (line) (mandatory) – the value of an arbitrary field.
$single (logical) – if true – returns the term, false - array, default is false.
A final example for displaying the contents of a metabox in a template:
1 2 3 4 5 6 7 8 |
/* * $obj->prefix я взяв із попереднього прикладу, * можна також вказати префікс самому: * get_post_meta($post->ID, '_seo_title', true); */ $title = ''; $title .= get_post_meta($post->ID, $obj->prefix.'title', true); $title .=''; |
I would also like to emphasize that the data recorded in the metabox should be stored in "Arbitrary fields", and what I indicated above is the output of the same arbitrary fields.
/*
- Basic web design course;
- Site layout;
- General course on CMS WordPress and continuation of the course on template development;
- Website development in PHP.