Home > AI > Backend > Wordpress >

add_meta_box

add_meta_box( string $id, string $title, callable $callback, string|array|WP_Screen $screen = null, string $context = 'advanced', string $priority = 'default', array $callback_args = null )

Adds a meta box to one or more screens.

Parameters 
$id
(string) (Required) Meta box ID (used in the 'id' attribute for the meta box).

$title
(string) (Required) Title of the meta box.

$callback
(callable) (Required) Function that fills the box with the desired content. The function should echo its output.

$screen
(string|array|WP_Screen) (Optional) The screen or screens on which to show the box (such as a post type, 'link', or 'comment'). Accepts a single screen ID, WP_Screen object, or array of screen IDs. Default is the current screen. If you have used add_menu_page() or add_submenu_page() to create a new screen (and hence screen_id), make sure your menu slug conforms to the limits of sanitize_key() otherwise the 'screen' menu may not correctly render on your page.

Default value: null

$context
(string) (Optional) The context within the screen where the box should display. Available contexts vary from screen to screen. Post edit screen contexts include 'normal', 'side', and 'advanced'. Comments screen contexts include 'normal' and 'side'. Menus meta boxes (accordion sections) all use the 'side' context. Global

Default value: 'advanced'

$priority
(string) (Optional) The priority within the context where the box should show. Accepts 'high', 'core', 'default', or 'low'.

Default value: 'default'

$callback_args
(array) (Optional) Data that should be set as the $args property of the box array (which is the second parameter passed to your callback).

Default value: null

Example 1: a simple input text

add_action('add_meta_boxes', 'st_meta_boxes');
        function st_meta_boxes() {
            add_meta_box(
                'st_mb_latex',          //$id
                __( 'Post Metas', 'jobyme88' ),  //$title
                'st_mb_callback',       //$callback
                'st_ai',                //$screen
                'side',                 //$context
                'high'                  //$priority
            );
        }
         
        function st_mb_callback(){
            global $post;
            $mb_latex_key = 'st_mb_latex_field';
            

            $metas = get_post_custom($post->ID);
            $value = isset($metas[$mb_latex_key]) ? esc_attr($metas[$mb_latex_key][0]) : "default";
            echo '<input name="'.$mb_latex_key.'" value="'.$value.'" />';

        }


        add_action( 'save_post', 'st_mb_save' );
        function st_mb_save($post_id) {
            $mb_latex_key = 'st_mb_latex_field';


            if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
                return $post_id;
            }

            

            // create a new post, this function will be executed too. At this time, $_POST is an empty array.
            // click update / publish, this function get executed.
            if (isset($_POST[$mb_latex_key])) {
                update_post_meta($post_id, $mb_latex_key, $_POST[$mb_latex_key]);
            }
            
        }

Example 2: checkbox

add_action('add_meta_boxes', 'st_meta_boxes');
        function st_meta_boxes() {
            add_meta_box(
                'st_mb_latex',          //$id
                __( 'Post Metas', 'jobyme88' ),  //$title
                'st_mb_callback',       //$callback
                'st_ai',                //$screen
                'side',                 //$context
                'high'                  //$priority
            );
        }
         
        function st_mb_callback(){
            global $post;
            $mb_latex_key = 'st_mb_latex_field';
            

            $metas = get_post_custom($post->ID);
            $value = isset($metas[$mb_latex_key]) ? esc_attr($metas[$mb_latex_key][0]) : false;
            echo '<label><input type="checkbox" name="'.$mb_latex_key.'" value="no_matter" '.checked($value, true, false).'/>Enable latex</label>';

        }


        add_action( 'save_post', 'st_mb_save' );
        function st_mb_save($post_id) {
            $mb_latex_key = 'st_mb_latex_field';


            if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
                return $post_id;
            }

            

            // create a new post, this function will be executed too. At this time, $_POST is an empty array.
            // click update / publish, this function get executed.
            if (isset($_POST[$mb_latex_key])) {
                update_post_meta($post_id, $mb_latex_key, true);
            } else {
                // first creat post
                // unchecked
                update_post_meta($post_id, $mb_latex_key, false);
            }
            
        }

Leave a Reply