How can I add extra attribute in the ‘Page Attribute’ section in wp-admin for pages?

I would like to add a select list in the Page Attribute box you see on the right side in the administration interface for pages.

Does anyone know if that is possible?

Read More

Or at least add a custom box below this box?

Related posts

Leave a Reply

2 comments

  1. There are no convenient hooks to add something to that box.

    You could do one of two things.

    1. Add a new Meta Box

    You do this by hooking into the add_meta_boxes action and calling add_meta_box. You can specify a callback function in your call to add_meta_box. That callback will take care of echoing out your select list.

    <?php
    add_action( 'add_meta_boxes', 'wpse44966_add_meta_box' );
    /**
     * Adds the meta box to the page screen
     */
    function wpse44966_add_meta_box()
    {
        add_meta_box(
            'wpse44966-meta-box', // id, used as the html id att
            __( 'WPSE 44966 Meta Box' ), // meta box title, like "Page Attributes"
            'wpse44966_meta_box_cb', // callback function, spits out the content
            'page', // post type or page. We'll add this to pages only
            'side', // context (where on the screen
            'low' // priority, where should this go in the context?
        );
    }
    
    /**
     * Callback function for our meta box.  Echos out the content
     */
    function wpse44966_meta_box_cb( $post )
    {
        // create your dropdown here
    }
    

    2. Remove the Default Page attributes meta box, add your own version

    All the content on the post editting screen, with the exception of the main editor and title area, is a meta box. You can remove them by calling remove_meta_box, then replace them with your own.

    So, first up, modify the add function above to include a remove meta box call. Then you’ll need to copy the page_attributes_meta_box function body from wp-admin/includes/meta-boxes.php and put your stuff below it.

    <?php
    add_action( 'add_meta_boxes', 'wpse44966_add_meta_box' );
    /**
     * Adds the meta box to the page screen
     */
    function wpse44966_add_meta_box( $post_type )
    {
        // remove the default
        remove_meta_box(
            'pageparentdiv',
            'page',
            'side'
        );
    
        // add our own
        add_meta_box(
            'wpse44966-meta-box',
            'page' == $post_type ? __('Page Attributes') : __('Attributes'),
            'wpse44966_meta_box_cb', 
            'page', 
            'side', 
            'low'
        );
    }
    
    /**
     * Callback function for our meta box.  Echos out the content
     */
    function wpse44966_meta_box_cb( $post )
    {
        // Copy the the `page_attributes_meta_box` function content here
        // add your drop down
    }
    

    Either way you do this, you’ll need to hook into save_post to save the value of your field with add_post_meta and/or update_post_meta.

    <?php
    add_action( 'save_post', 'wpse44966_save_post' );
    /**
     * Save our custom field value
     */
    function wpse44966_save_post( $post_id )
    {
        // check nonces, permissions here
        // save the data with update_post_meta
    }
    

    This tutorial might help you out.

  2. Woohoo! As of WP 4.9.0, there is an action hook to add code to the Page Attributes meta box: 'page_attributes_misc_attributes'. See the Codex for reference.

    Example:

    function wpse44966_page_attributes( $post ) {
    
        wp_nonce_field( 'wpse44966_page_attributes_nonce', 'wpse44966_page_attributes_nonce' );
        ?>
        <p class="post-attributes-label-wrapper wpse44966-label-wrapper">
            <label class="wpse44966-label" for="wpse44966_option" style="font-weight: 600;"><?php _e( 'Option Label', 'textdomain' ); ?></label>
            <input id="wpse44966_option" name="wpse44966_option" type="checkbox" value="1" style="margin-left: 5px;"<?php if( isset( $post->wpse44966_option ) && $post->wpse44966_option ) echo ' checked="checked"'; ?>>
        </p>
        <?php
    }
    add_action( 'page_attributes_misc_attributes', 'wpse44966_page_attributes' );
    
    function wpse44966_metabox_save_post( $post_id ) {
        if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
        if( ! isset( $_POST['wpse44966_page_attributes_nonce'] ) || ! wp_verify_nonce( $_POST['wpse44966_page_attributes_nonce'], 'wpse44966_page_attributes_nonce' ) ) return;
        if( ! current_user_can( 'edit_post', $post_id ) ) return;
        $wpse44966_option = (int) ( isset( $_POST['wpse44966_option'] ) && $_POST['wpse44966_option'] );
        update_post_meta( $post_id, 'wpse44966_option', $wpse44966_option );
    }
    add_action( 'save_post', 'wpse44966_metabox_save_post' );