WordPress.org

Ready to get started?Download WordPress

Forums

WordPress default screen options (2 posts)

  1. enderoe
    Member
    Posted 10 months ago #

    Hi,

    I would like to uncheck certain screen options such as the sharing and excerpt fields.

    I already found some code wich i implemented into my functions.php but this isnt working:

    function set_user_metaboxes($user_id=NULL) {
        $post_types= array( 'post', 'page', 'link', 'attachment', 'product' );
        // add any custom post types here:
        // $post_types[]= 'my_custom_post_type';
        foreach ($post_types as $post_type) {
    
           // These are the metakeys we will need to update
           $meta_key= array(
               'order' => "meta-box-order_$post_type",
               'hidden' => "metaboxhidden_$post_type",
           );
    
           // The rest is the same as drebabels's code,
           // with '*_user_meta()' changed to '*_user_option()'
    
           // So this can be used without hooking into user_register
           if ( ! $user_id)
               $user_id = get_current_user_id(); 
    
           // Set the default order if it has not been set yet
           if ( ! get_user_option( $meta_key['order'], $user_id ) ) {
               $meta_value = array(
                   'side' => 'submitdiv,formatdiv,categorydiv,postimagediv',
                   'normal' => 'postexcerpt,tagsdiv-post_tag,postcustom,commentstatusdiv,commentsdiv,trackbacksdiv,slugdiv,authordiv,revisionsdiv',
                   'advanced' => '',
               );
               update_user_option( $user_id, $meta_key['order'], $meta_value, true );
           }
    
           // Set the default hiddens if it has not been set yet
           if ( ! get_user_option( $meta_key['hidden'], $user_id ) ) {
               $meta_value = array('postcustom','trackbacksdiv','commentstatusdiv','commentsdiv','slugdiv','authordiv','revisionsdiv' , 'avia_hero' );
               update_user_option( $user_id, $meta_key['hidden'], $meta_value, true );
           }
        }
     }
  2. Luke Janicke
    Member
    Posted 1 month ago #

    Add one of the following hooks with the function (before or after, doesn’t matter).

    add_action( 'add_meta_boxes_post', 'set_user_metaboxes', 10, 1 );

    This runs when the post edit screen is loading.

    add_action( 'user_register', 'set_user_metaboxes', 10, 1 );

    This runs when a new user is created.

    The second might be better. Your code (which I am also using) only changes a users settings (show/hide and order) if the user hasn’t already altered them from the new user defaults. Running this action every time a user opens a post edit page is pointless.

    If you want to force the defaults on every page load, the code needs altering.

Reply

You must log in to post.

About this Topic