# Custom Meta Boxes for post (ok)

![](/files/-MPeL9idTYhqaVBgCH33)

![](/files/-MPeLCKBkr17B5IF-hRq)

C:\xampp\htdocs\wordpress\wp-content\themes\twentytwentyone\functions.php

```
<?php  
function wporg_add_custom_box() {
    $screens = [ 'post', 'wporg_cpt' ];
    foreach ( $screens as $screen ) {
        add_meta_box(
            'wporg_box_id',                 // Unique ID
            'Custom Meta Box Title',      // Box title
            'wporg_custom_box_html',  // Content callback, must be of type callable
            $screen                            // Post type
        );
    }
}
add_action( 'add_meta_boxes', 'wporg_add_custom_box' );
function wporg_custom_box_html( $post ) {
    $value = get_post_meta( $post->ID, '_wporg_meta_key', true );
    ?>
    <label for="wporg_field">Description for this field</label>
    <select name="wporg_field" id="wporg_field" class="postbox">
        <option value="">Select something...</option>
        <option value="aaaaaaaaaaagggggggggghhhhhhhhhh" <?php selected( $value, 'aaaaaaaaaaagggggggggghhhhhhhhhh' ); ?>>aaaaaaaaaaagggggggggghhhhhhhhhh</option>
        <option value="else" <?php selected( $value, 'else' ); ?>>Else</option>
    </select>
    <?php
}
function wporg_save_postdata( $post_id ) {
    if ( array_key_exists( 'wporg_field', $_POST ) ) {
        update_post_meta(
            $post_id,
            '_wporg_meta_key',
            $_POST['wporg_field']
        );
    }
}
add_action( 'save_post', 'wporg_save_postdata' );
function wporg_meta_box_scripts() {
     // Get current admin screen, or null.
    $screen = get_current_screen();
    // Verify admin screen object before use.
    if ( is_object( $screen ) ) {
        // Enqueue only for specific post types.
        if ( in_array( $screen->post_type, [ 'post', 'wporg_cpt' ], true ) ) {
            wp_enqueue_script( 'wporg_meta_box_script', plugin_dir_url( __FILE__ ) . 'admin/meta-boxes/js/admin.js', [ 'jquery' ], '1.0.0', true );
            wp_localize_script(
                'wporg_meta_box_script',
                'wporg_meta_box_obj',
                [
                    'url' => admin_url( 'admin-ajax.php' ),
                ]
            );
        }
    }
}
add_action( 'admin_enqueue_scripts', 'wporg_meta_box_scripts' );
?>
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://learnphp.gitbook.io/learnphp/wordpress-advand/custom-meta-boxes-for-post-ok.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
