1

I am wondering how to add meta box in WordPress specific page admin, I mean when I create meta_box with code snippet below provided from source tutorial is really perfect and effective but one thing that I need to control is display that meta_box only in specific page for instance: pretend I have two page in my WordPress project named Home and About.

When I create meta_box by default the meta_box that I add will display on the same page admin back end, imagined when I clicked edit button to Home and About page my meta box will appear in both page. What I want is make a meta_box only show in “Home” admin backend page when I click the edit page.

My goal is set different meta_box in every different page, meaning the users specially the blog editor expect different meta_box_field in different page when they click edit button in each page, That is the thing I can’t figure out can you help me to solve that problem

/*
Plugin Name: Meta Box Example
Description: Example demonstrating how to add Meta Boxes.
Plugin URI:  https://plugin-planet.com/
Author:      Jeff Starr
Version:     1.0
*/

// register meta box
function myplugin_add_meta_box() {

    $post_types = array( 'post', 'page' );

    foreach ( $post_types as $post_type ) {

        add_meta_box(
            'myplugin_meta_box',         // Unique ID of meta box
            'MyPlugin Meta Box',         // Title of meta box
            'myplugin_display_meta_box', // Callback function
            $post_type                   // Post type
        );

    }

}
add_action( 'add_meta_boxes', 'myplugin_add_meta_box' );

// display meta box
function myplugin_display_meta_box( $post ) {

    $value = get_post_meta( $post->ID, '_myplugin_meta_key', true );

    wp_nonce_field( basename( __FILE__ ), 'myplugin_meta_box_nonce' );

    ?>

    <label for="myplugin-meta-box">Field Description</label>
    <select id="myplugin-meta-box" name="myplugin-meta-box">
        <option value="">Select option...</option>
        <option value="option-1" <?php selected( $value, 'option-1' ); ?>>Option 1</option>
        <option value="option-2" <?php selected( $value, 'option-2' ); ?>>Option 2</option>
        <option value="option-3" <?php selected( $value, 'option-3' ); ?>>Option 3</option>
    </select>

<?php

}

// save meta box
function myplugin_save_meta_box( $post_id ) {

    $is_autosave = wp_is_post_autosave( $post_id );
    $is_revision = wp_is_post_revision( $post_id );

    $is_valid_nonce = false;

    if ( isset( $_POST[ 'myplugin_meta_box_nonce' ] ) ) {

        if ( wp_verify_nonce( $_POST[ 'myplugin_meta_box_nonce' ], basename( __FILE__ ) ) ) {

            $is_valid_nonce = true;

        }

    }

    if ( $is_autosave || $is_revision || !$is_valid_nonce ) return;

    if ( array_key_exists( 'myplugin-meta-box', $_POST ) ) {

        update_post_meta(
            $post_id,                                            // Post ID
            '_myplugin_meta_key',                                // Meta key
            sanitize_text_field( $_POST[ 'myplugin-meta-box' ] ) // Meta value
        );

    }

}
add_action( 'save_post', 'myplugin_save_meta_box' );

Is there any condition or function to do that you will recommend?

2 Answers 2

2

/* Add meta boxs for particular pages */

function meta_set_particular_page() {
    $post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'];
    $current_page_title = get_the_title($post_id);
    if ($current_page_title == 'home') {
        add_meta_box('Home_page', 'Home Name:', 'only_home', 'page', 'side', 'core');
    }
    if($current_page_title == 'about'){
        add_meta_box('About_page', 'About Name:', 'only_about', 'page', 'side', 'core');
    }
}
add_action('add_meta_boxes', 'meta_set_particular_page');

/* Add custom meta box for home page */

function only_home($post) {
    $home_page = esc_html(get_post_meta($post->ID, 'home_page', true));
    ?>
    <table style="width:100%;">
        <tr>
            <td style="width: 20%">Name</td>
            <td style="width: 40%">
                <input type="text" size="70" name="home_page" placeholder="Home Name" value="<?php echo $home_page; ?>">
            </td>
        </tr>
    </table>
    <?php
}

/*Add custom meta box for about page*/

function only_about($post) {
    $about_page = esc_html(get_post_meta($post->ID, 'about_page', true));
    ?>
    <table style="width:100%;">
        <tr>
            <td style="width: 20%">Name</td>
            <td style="width: 40%">
                <input type="text" size="50" name="about_page" placeholder="About Name" value="<?php echo $about_page; ?>">
            </td>
        </tr>
    </table>
    <?php
}

/*Save custom post meta values*/

function custom_metabox_fields($custom_metabox_id) {
    if (isset($_POST['home_page'])) {
        update_post_meta($custom_metabox_id, 'home_page', $_POST['home_page']);
    }
     if (isset($_POST['about_page'])) {
        update_post_meta($custom_metabox_id, 'about_page', $_POST['about_page']);
    }
}
add_action('save_post', 'custom_metabox_fields', 10, 2);
2
  • Hey, @vishwa nice code. Your answer is off to a good start. Please consider explaining how and why this code solves the problem so others can learn from it. Check the guide to answers for further advice. Apr 3, 2021 at 19:35
  • You are awesome! Thanks you save my day! Apr 5, 2021 at 9:26
1
**check via post ID for a specific page or post**


$post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'] ;
if ($post_id == '84')
{
    add_meta_box();
}

**OR
check via template page name just as below**



$template_file = get_post_meta($post_id,'_wp_page_template',TRUE);
if ($template_file == 'home.php')
{
    add_meta_box();
}
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.