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.

 

pageparentdiv – метабокс с выбором родительской страницы и шаблона страниц wordpress tagsdiv-post_tag – метабокс метки wordpress

Before creating your metaboxes, it is necessary to learn to remove the extra ones.

remove_meta_box() – removal of the metabox.

$id (line) – metabox identifier, it can be found in the source code page, I'll list all the standard metaboxes below.
$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.

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.

commentstatus metabox

commentsdiv – displays comments on the current position.

commentsdiv metabox стандартные метабоксы WordPress

slugdiv – record shortcut.

metabox slug - метабокс язык записи WordPress

audit div - editors.

revisionsdiv – редакции.

authordiv - the author of the record.

author – автор записи

postcustom - addition / editing of arbitrary fields.

postcustom – добавление/редактирование произвольных полей WordPress

postexcerpt - quote.

postexcerpt – метабокс wordpress цитата

trackbacksdiv – backlinks.

trackbacksdiv – метабокс обратные ссылки в wordpress

categorydiv – metabox list of WordPress headings.

categorydiv – метабокс список рубрик

tagsdiv-post_tag - labels.

tagsdiv-post_tag – метабокс метки wordpress

postimagediv – record thumbnail.

postimagediv – миниатюра записи wordpress.

pageparentdiv – metabox with selection of parent page and page template.

pageparentdiv – метабокс с выбором родительской страницы и шаблона страниц wordpress

submitdiv - a block with a "publish" button.

submitdiv – метабокс с кнопкой «опубликовать».

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.

The line Support shows, which metaboxes the new data type should use.

You can also add a category and tags for a new data type using an array taxonomies.

Full code view for our new data type:

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:

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.

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.

$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:

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:

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’ ] . ‘

‘; echo ‘

 

‘; break; } default: { // default type="text" echo '

‘ . $option[ ‘description’ ] . ‘

‘; echo ‘‘; break; } } ?>

Creating a class object:
So much code... The question arises - why all this? Apparently, the previous method was simpler.

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:

Reference to metadata (arbitrary fields) post / pages

$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:

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.


Buy WordPress hosting
/* WordPress tutor
Online tutoring services. List of courses I teach
  • Basic web design course;
  • Site layout;
  • General course on CMS WordPress and continuation of the course on template development;
  • Website development in PHP.
Read more on the page WordPress tutor
*/

Nikolaenko Maxim

Director of web studies ProGrafika. I am developing, website design and promotion. Always glad to new blog readers and good clients.


You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Templates for WordPress
The best hosting in Ukraine
Stable hosting for Drupal