Set Default Listing “View” in Admin

Although having the feature of persistent settings in core is nice, it may take quite a while before it’s actually accepted. WordPress 3.5 is still quite far away.

So let’s augment the global $_REQUEST array instead.

add_action( 'load-edit.php', 'wpse34956_force_excerpt' );
function wpse34956_force_excerpt() {
    $_REQUEST['mode'] = 'excerpt';
}

This will lock up modes, forcing excerpt mode all the time, so let’s turn let the user decide but keep it persistent using the user’s metadata:

add_action( 'load-edit.php', 'wpse34956_persistent_posts_list_mode' );
function wpse34956_persistent_posts_list_mode() {
    if ( isset( $_REQUEST['mode'] ) ) {
        // save the list mode
        update_user_meta( get_current_user_id(), 'posts_list_mode', $_REQUEST['mode'] );
        return;
    }
    // retrieve the list mode
    if ( $mode = get_user_meta( get_current_user_id(), 'posts_list_mode', true ) )
        $_REQUEST['mode'] = $mode;
}

You can further interpolate post_type into all by taking into account the $_GET['post_type'] variable when available.

add_action( 'load-edit.php', 'wpse34956_persistent_posts_list_mode' );
function wpse34956_persistent_posts_list_mode() {

    // take into account post types that support excerpts
    $post_type = isset( $_GET['post_type'] ) ? $_GET['post_type'] : '';
    if ( $post_type && !post_type_supports( $post_type, 'excerpt' ) )
        return; // don't care

    if ( isset( $_REQUEST['mode'] ) ) {
        // save the list mode
        update_user_meta( get_current_user_id(), 'posts_list_mode' . $post_type, $_REQUEST['mode'] );
        return;
    }

    // retrieve the list mode
    if ( $mode = get_user_meta( get_current_user_id(), 'posts_list_mode' . $post_type, true ) )
        $_REQUEST['mode'] = $mode;
}

Viola! Persistent list mode per post type per user, no hacks.

Leave a Comment