WordPress Custom Meta Box

Creating interfaces

To create a new wordpress custom meta box two custom functions are needed. The first function is the main one, it creates a new meta box (defines the name and label) and calls the second one, which serves to display the inside of the meta box.

Outline of the meta box

In the previous example, a custom function wpp_add_meta_box() was created to activate the meta box, and within it the wordpress function add_meta_box() is used:

add_meta_box()

Where are the parameters:

  • $id
    (string) (required) HTML ‘id’ attribute
  • $title
    (string) (required) The name visible to the end user
  • $callback
    (callback) (Required) A function that prints HTML to the screen. Accepts max two arguments:
    1.) $post object for the currently viewed post or page
    2.) $metabox (string)
  • $screen
    (string) (optional) The type on which the meta box will appear (Can be: ‘post’,’page’,’dashboard’,’link’,’attachment’,’custom_post_type’,’comment’ where custom_post_type is the custom post type slug)
  • $context
    (string) (optional) The part of the screen where the “edit” section is displayed (‘normal’, ‘advanced’, or ‘side’).
  • $priority
    (string) (Optional) Priority that determines where it will be displayed (‘high’, ‘core’, ‘default’ or ‘low’)
  • $callback_args
    (array) (Optional) Arguments to pass to the callback function. The callback function will receive a $post object with the arguments passed.

Inside the Meta box

This second function, the so-called The callback function creates the inner form of the meta box and is called from the first function that creates the meta box frame. The core of the interior is usually an HTML form that allows data entry.

For the sake of security, it is necessary to use protection against malicious input, so we will create a nonce within the function, which will be verified later before saving the data to the database.

If the value of the field is entered and saved in the database, it is necessary that it be visible in the interface, i.e. so that the value does not disappear from the field after the SAVE action. For this reason, it is necessary to add a piece of code that returns the value of the field to the field.

Example No. 1

When all the above is taken into account, the function would look like this:

Example No. 2

If inside the meta box an input field is used to enter the date, then for a good “user experience” it would be good to use a date picker. In the following example, we will use the date picker from the jQuery UI library.

Saving data of custom fields

We need to verify this came from our screen and with proper authorization, because the save_post action can be triggered at other times.

Meta box generator

Online generator

You can see an excellent WordPress code generator on the
page
http://jeremyhixon.com/tool/wordpress-meta-box-generator-v2-beta/

Here is the generated code for one label and one check box:

ACF plugin

ACF is a plugin that creates meta boxes and customized custom fields inside them.