3

By default in the admin the Excerpt is hidden. See below.

By default in the admin the Excerpt is hidden

I would like to make it show up by default.

4

The names of unchecked boxes in Screen Options for Edit Post screen are stored in user's meta, per individual user, as an array. Insert the following code in your theme's functions.php:

function wpse_edit_post_show_excerpt( $user_login, $user ) {
    $unchecked = get_user_meta( $user->ID, 'metaboxhidden_post', true );
    $key = array_search( 'postexcerpt', $unchecked );
    if ( FALSE !== $key ) {
        array_splice( $unchecked, $key, 1 );
        update_user_meta( $user->ID, 'metaboxhidden_post', $unchecked );
    }
}
add_action( 'wp_login', 'wpse_edit_post_show_excerpt', 10, 2 );

This will update user's meta ( after successful login ) by removing postexcerpt name from the array of unchecked boxes names.

Note: to avoid losing your change, create a child theme and put the code into its functions.php.

1
  • Thank you, Frank! For reference, if anyone ever reads this down the road and wants to make sure this change is reflected immediately using this method I'd just change the SALTs in the wp-config.php to force everyone to login again and reflect these changes. Cheers! – Stephen Sabatini Aug 7 '17 at 7:58
3

Sharing a slight modification of Franks' solution. In my case, I don't want users to ever hide the excerpt so I've hooked the function to the admin_init instead of wp_login.

Frank's function executes when the user logs into the site which means that once the user is logged in they can hide it again. This solution will fire every time an admin page is loaded which wilmake it impossible for the user to hide the field.

function wpse_edit_post_show_excerpt() {
  $user = wp_get_current_user();
  $unchecked = get_user_meta( $user->ID, 'metaboxhidden_post', true );
  $key = array_search( 'postexcerpt', $unchecked );
  if ( FALSE !== $key ) {
    array_splice( $unchecked, $key, 1 );
    update_user_meta( $user->ID, 'metaboxhidden_post', $unchecked );
  }
}
add_action( 'admin_init', 'wpse_edit_post_show_excerpt', 10 );
8
  • The above one won't allow users to choose regardless. If they logout and login, then it will reset, not keeping their preferences. This answer makes more sense. I'd be interested to see if there's a way to hide the checkbox from the Screen Options. I might post a related question soon. – Stephen Sabatini Oct 6 '17 at 13:47
  • @StephenSabatini Add a link to the new question here once posted, if you you don't mind. – Dedering Oct 6 '17 at 14:06
  • @StephenSabatini See response – Dedering Oct 9 '17 at 21:11
  • Accepted your answer on the other post. For this question, for some reason it just started giving warnings and isn't working. Warning: array_search() expects parameter 2 to be array, boolean given in /wp-content/themes/theme-name/functions.php on line 315. Warning: array_splice() expects parameter 1 to be array, boolean given in /wp-content/themes/theme-name/functions.php on line 317. – Stephen Sabatini Oct 12 '17 at 15:56
2

neither solution worked for me - but this "duct tape" css fix worked for me:

/* always show excerpt .. hide display options */
add_action('admin_head', 'myplugin_modify_admin_header');
function myplugin_modify_admin_header() {
  ?>
  <style type='text/css'> 
    #postexcerpt { display: block !important; } 
    label[for=postexcerpt-hide] { display: none !important; }
  </style>
  <?php
}
1

While user meta stores unchecked meta boxes as an array, it does not store anything if the user has not modified the original defaults, returning a blank string instead. This results in errors on login if not accounted for, so I added an extra function & hook to cover all bases. I also wrapped the unchecked variable with a conditional to make sure it didn't error if nothing was returned. Thanks to Role-Editor for the additional function.

function wpse_edit_post_show_excerpt() {
    $user = wp_get_current_user();
    $unchecked = get_user_meta( $user->ID, 'metaboxhidden_post', true );
    if(!empty($unchecked)){
        $key = array_search( 'postexcerpt', $unchecked );
        if ( FALSE !== $key ) {
            array_splice( $unchecked, $key, 1 );
            update_user_meta( $user->ID, 'metaboxhidden_post', $unchecked );
        }
    }
}
add_action( 'admin_init', 'wpse_edit_post_show_excerpt', 10 );

function show_excerpt_meta_box($hidden, $screen) {
    if ( 'post' == $screen->base ) {
        foreach($hidden as $key=>$value) {
            if ('postexcerpt' == $value) {
                unset($hidden[$key]);
                break;
            }
        }
    }
    return $hidden;
}
add_filter( 'default_hidden_meta_boxes', 'show_excerpt_meta_box', 10, 2 );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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