Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

Or at least add a custom box below this box?

share|improve this question
add comment

1 Answer 1

up vote 8 down vote accepted

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.

share|improve this answer
    
Thanks for a great answer. I was hoping for some sort of a hook, but this will do just fine :) –  Steven Mar 8 '12 at 21:53
    
excellent solution and good full answer - reference link fills in the gaps - thanks –  Q Studio Dec 5 '12 at 13:08
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.