add new metabox to page editor with just page parent

I run a multisite install and I have removed the page attributes meta box from view for my users because I don’t want them to be able to change templates. I would like them to be able to change the page parent though. I need to add a new meta box for pages that adds just the page parent selector back.

Related posts

Leave a Reply

2 comments

  1. Did just that for one of my plugins, recently…

    function render($post) {
        $pages = wp_dropdown_pages(array('post_type' => 'page', 'exclude_tree' => $post->ID, 'selected' => $post->post_parent, 'name' => 'parent_id', 'show_option_none' => __('(no parent)'), 'sort_column'=> 'menu_order, post_title', 'echo' => 0));
        if (!empty($pages)) {
            ?>
            <p><strong><?php _e('Parent') ?></strong></p>
            <label class="screen-reader-text" for="parent_id"><?php _e('Landing Page') ?></label>
            <?php echo $pages; ?>
            <?php
        }
        ?>
        <?php
    }
    

    I implement my metaboxes as classes, hence the funny function name.
    I removed the menu order field which i originally kept in my metabox. I think it’s a good idea to keep it.

    You’ll find the original ‘attributes’ metabox in wp-admin/includes/meta-boxes.php, btw., which is a nice library of code to have a look at if you implement custom metaboxes.

  2. well thanks to the help of @wyrfel I was able to get this working. I had to edit the code you gave me some, and the code for adding meta boxes since this function is already part of the core. for anyone else looking here is what I finally used (whole thing added to theme’s functions.php file)

    // add page parent metabox
    define('MY_WORDPRESS_FOLDER',$_SERVER['DOCUMENT_ROOT']);
    define('MY_THEME_FOLDER',str_replace("",'/',dirname(__FILE__)));
    define('MY_THEME_PATH','/' . substr(MY_THEME_FOLDER,stripos(MY_THEME_FOLDER,'wp-content')));
    
    add_action('admin_init','my_meta_init');
    
    function my_meta_init()
    { 
        // add a meta box for each of the wordpress page types: pages
        foreach (array('page') as $type) 
        {
            add_meta_box('my_all_meta', 'Page Parent', 'my_meta_setup', $type, 'side', 'low');
        }
    
        // add a callback function to save any data a user enters in
        //add_action('save_post','my_meta_save');
    }
    
    function my_meta_setup($post) {
    $post_type_object = get_post_type_object($post->post_type);
        if ( $post_type_object->hierarchical ) {
            $pages = wp_dropdown_pages(array('post_type' => $post->post_type, 'exclude_tree' => $post->ID, 'selected' => $post->post_parent, 'name' => 'parent_id', 'show_option_none' => __('(no parent)'), 'sort_column'=> 'menu_order, post_title', 'echo' => 0));
            if ( ! empty($pages) ) {
    ?>
    <p><strong><?php _e('Parent for this page:') ?></strong></p>
    <label class="screen-reader-text" for="parent_id"><?php _e('Parent') ?></label>
    <?php echo $pages; 
            }
        }
    }