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

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.

Tags:

Custom Field