• I’ve set up a custom meta box (according to a tutorial) so that it appears on the Edit Post screen. This is working well.

    I’d like to know how to modify the code below so that the custom meta box would appear in the Edit Page screen instead. I tried changing the 4th parameter of the add_meta_box() function from ‘post’ to ‘page’, but I still can’t get the custom meta box to appear in the Edit Page screen.

    Your help would be greatly appreciated.

    <?php
    
    /*
    Plugin Name: Custom Meta Box Template
    Plugin URI: http://example.com/
    Description: Provides a starting point for creating custom meta boxes.
    Author: Author
    Version: 1.0
    Author URI: http://example.com/
    */
    
    /**
     * Adds a meta box to the post editing screen
     */
    function prfx_custom_meta() {
        add_meta_box( 'prfx_meta', __( 'Meta Box Title', 'prfx-textdomain' ), 'prfx_meta_callback', 'post' );
    }
    add_action( 'add_meta_boxes', 'prfx_custom_meta' );
    
    /**
     * Outputs the content of the meta box
     */
    function prfx_meta_callback( $post ) {
        wp_nonce_field( basename( __FILE__ ), 'prfx_nonce' );
        $prfx_stored_meta = get_post_meta( $post->ID );
        ?>
    
            <label for="meta-text" class="prfx-row-title"><?php _e( 'Example Text Input', 'prfx-textdomain' )?></label>
            <input type="text" name="meta-text" id="meta-text" value="<?php if ( isset ( $prfx_stored_meta['meta-text'] ) ) echo $prfx_stored_meta['meta-text'][0]; ?>" />
    
        <?php
    }
    
    /**
     * Saves the custom meta input
     */
    function prfx_meta_save( $post_id ) {
    
        // Checks save status
        $is_autosave = wp_is_post_autosave( $post_id );
        $is_revision = wp_is_post_revision( $post_id );
        $is_valid_nonce = ( isset( $_POST[ 'prfx_nonce' ] ) && wp_verify_nonce( $_POST[ 'prfx_nonce' ], basename( __FILE__ ) ) ) ? 'true' : 'false';
    
        // Exits script depending on save status
        if ( $is_autosave || $is_revision || !$is_valid_nonce ) {
            return;
        }
    
        // Checks for input and sanitizes/saves if needed
        if( isset( $_POST[ 'meta-text' ] ) ) {
            update_post_meta( $post_id, 'meta-text', sanitize_text_field( $_POST[ 'meta-text' ] ) );
        }
    
    }
    add_action( 'save_post', 'prfx_meta_save' );
    
    /**
     * Adds the meta box stylesheet when appropriate
     */
    function prfx_admin_styles(){
        global $typenow;
        if( $typenow == 'post' ) {
            wp_enqueue_style( 'prfx_meta_box_styles', plugin_dir_url( __FILE__ ) . 'meta-box-styles.css' );
        }
    }
    add_action( 'admin_print_styles', 'prfx_admin_styles' );

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    Changing the parameter to page is all you should need to do. You may need to check your meta box’s checkbox in the screen options pull down at the top of the page edit screen to actually see it.

    Thread Starter alexWP333

    (@alexwp333)

    It turns out that I forgot to activate the plugin for the new meta box. My weekend brain was on vacation.

    Thanks again for your help.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Custom Meta Box for Edit Page Screen’ is closed to new replies.