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 am desperately looking for some way (any way) to set default "screen options" and metabox order through the functions.php file. does anyone have any help they can provide here?

share|improve this question

2 Answers 2

up vote 21 down vote accepted

Setting the order of meta boxes on post edit pages

You will need to remove the meta boxes, and add them again in the order you want.

To disable meta boxes: (customize to your needs, look at the metabox id in the html code to know which name you should use as first parameter of the functions)

function my_remove_meta_boxes() {

    remove_meta_box('postcustom', 'post', 'core');

    remove_meta_box('commentsdiv', 'post', 'core');

    ...
}

add_action( 'admin_menu', 'my_remove_meta_boxes' );

After removing them, you can use the add_meta_box function to add them at new positions: http://codex.wordpress.org/Function_Reference/add_meta_box. The order of the meta boxes depends on the order in which you call the add_meta_box function. E.g.: with the following code snippet, the comments meta box will be before the custom fields meta box.

function my_add_meta_boxes( $post_type, $post ) {
    if ( ('publish' == $post->post_status || 'private' == $post->post_status) && post_type_supports($post_type, 'comments') )
        add_meta_box('commentsdiv', __('Comments'), 'post_comment_meta_box', $post_type, 'normal', 'core');

    if ( post_type_supports($post_type, 'custom-fields') )
        add_meta_box('postcustom', __('Custom Fields'), 'post_custom_meta_box', $post_type, 'normal', 'core');

    ...
}

add_action( 'add_meta_boxes', 'my_add_meta_boxes' );

You may want to look at the file wp-admin/edit-form-advanced.php

Setting which columns title show up on the post list page

You need to use the filter manage_{$post_type}_posts_columns. E.g.: the following snippet will remove the comments column.

function my_remove_columns( $posts_columns ) {
    unset( $posts_columns['comments'] );

    return $posts_columns;
}

add_filter( 'manage_post_posts_columns', 'my_remove_columns' );

Setting the default results to be shown on the post list page

Use the filters 'edit_{$post_type}_per_page' and 'edit_posts_per_page'.

function my_edit_post_per_page( $per_page, $post_type ) {

    $edit_per_page = 'edit_' . $post_type . '_per_page';
    $per_page = (int) get_user_option( $edit_per_page );
    if ( empty( $per_page ) || $per_page < 1 )
        $per_page = 1;

    return $per_page;
}

add_filter( 'edit_posts_per_page', 'my_edit_post_per_page' );

To target a specific post type:

  • use add_filter( 'edit_{post type}_per_page', 'my_edit_post_per_page' ); e.g. add_filter( 'edit_post_per_page', 'my_edit_post_per_page' ); for posts, add_filter( 'edit_page_per_page', 'my_edit_post_per_page' ); for pages.

  • or use a condition within your function. e.g.:

    function my_edit_post_per_page( $per_page, $post_type ) {

    if( $post_type == 'post' ) {
        $edit_per_page = 'edit_' . $post_type . '_per_page';
        $per_page = (int) get_user_option( $edit_per_page );
        if ( empty( $per_page ) || $per_page < 1 )
            $per_page = 1;
    }
    
    
    return $per_page;
    
share|improve this answer
    
Sorry, maybe i did not explain myself correctly. I was referring to the meta boxes on the post edit page in the admin area as well as setting the default number of posts to display on the admin post list page. –  NetConstructor.com Sep 6 '10 at 8:13
1  
so technically this is not a question, it is really 2 questions? –  Tom J Nowell Sep 6 '10 at 8:18
    
I modified my answer. Let me know if you need more details. –  sorich87 Sep 6 '10 at 8:41
    
@sorich87 thank you for your response however from what I remember there was no way to specific the exact order of each metabox or was there? Maybe I should be a bit more specific. I am basically looking for various code snippets which let me pre-set various options for all users without having to login to each account. Examples: setting the exact order of meta boxes on post edit pages, setting which columns title show up on the post list page, setting the default results to be shown on the post list page... things like that. Thanks in advance. –  NetConstructor.com Sep 7 '10 at 4:41
    
I modified my answer. –  sorich87 Sep 7 '10 at 7:17

In the following example, replace {CPT} with the post type name, like post, page, book, event, imaginary, foobar...

add_filter( 'get_user_option_meta-box-order_{CPT}', 'metabox_order' );
function metabox_order( $order ) {
    return array(
        'normal' => join( 
            ",", 
            array(       // vvv  Arrange here as you desire
                'customdiv-{CPT}',
                'authordiv',
                'slugdiv',
            )
        ),
    );
}

Notes:

  • Along with normal, you can also have side and advanced
  • The values are comma separated div ids, you can grab by view the page source code or you can guess
  • Any metabox that exists isn't included in your custom order will be added automatically at the end of your list.
share|improve this answer

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.