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?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
/* 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);
Method 2
**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();
}
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0