• niel

    (@nielfernandez)


    Hello WordPress Developer! Good afternoon Sir./Mam

    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

    Thanks in advance ..

    
    // <blockquote>Snippet adding meta box</blockquote>
    /*
    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' );
    • This topic was modified 3 years, 11 months ago by Jan Dembowski. Reason: Formatting
Viewing 6 replies - 1 through 6 (of 6 total)
  • Moderator bcworkz

    (@bcworkz)

    What I’d do is use one meta box that contextually displays different things depending on context. Of course you can manage what kind of screen a meta box appears on, but there’s no finer control within the meta box scheme itself.

    However, your meta box code can get values from $_GET and behave accordingly. For example, on the post edit screen, the current post’s ID is in $_GET['post']. Based on ID, your meta box could output different fields. Just be sure the code that processes meta box data on submit knows how to handle any of the possible fields.

    @bcworkz is this so you only have a single meta-box php file rather than say 4 different ones that all go on a different screen? So it’s easier to manage?

    Thread Starter niel

    (@nielfernandez)

    @bcworkz I can’t understand anything, Is there any condition or function to do that you will recommend?

    @visualeight have more idea to do that?

    Hey Niel, I’m not sure if you’ve used if or if/else statements to load custom scripts (css or js) on specific front-end pages (I do this quite often), but it’s the same idea. I would do a check to see what page is being loaded, use is_page() to test for what page you want that meta box on, if it’s not the page you want, just “return” so nothing runs. If it returns as true, meaning that it is the page you want, then register that meta box.

    I’ve not done it the way bcworkz mentioned, so I am interested to know more of why it would be beneficial. I have a custom project with tons of meta boxes across different post types, bcworkz pointed me in the right direction for creating them all.

    Thread Starter niel

    (@nielfernandez)

    @visualeight Thanks anyway its solved with this link here : https://wordpress.stackexchange.com/questions/114111/how-do-i-get-the-current-edit-page-id-in-the-admin

    Through :

    global $post;

    // Get the data
    $id = $post->ID;

    using if statement

    if($id == ‘yourPageId’) :
    // meta box goes here
    endif;

    @visualeight @bcworkz thanks to all of you! :))

    • This reply was modified 3 years, 11 months ago by t-p.
    • This reply was modified 3 years, 11 months ago by bcworkz. Reason: fixed link
    Moderator bcworkz

    (@bcworkz)

    niel, I’m glad you found the information you needed.

    To clarify my earlier thought for anyone interested, yes, the idea would be to minimize redundancy by handling all situations in one meta box. Based on the post ID determined as niel discovered, conditionally do one thing or another. You can use either a series of if/else if/else/then structures or a switch/case structure.

    Whether this makes more sense than multiple meta boxes depends on how much similarity or difference there is between meta content. Either way, you still need some conditional structure to decide what to do.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘add_meta_box (Will display only in specific page admin)’ is closed to new replies.