Skip to content

Instantly share code, notes, and snippets.

@rosswintle
Last active September 8, 2015 15:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rosswintle/60152c844a4f8aa1f773 to your computer and use it in GitHub Desktop.
Save rosswintle/60152c844a4f8aa1f773 to your computer and use it in GitHub Desktop.
Set default hidden meta boxes and meta box order for users in WordPress
<?php
/*
* This filter specifies default orderings for meta boxes on article screens.
*
* If a user-specific setting exists for the hidden/order list then that is used
*
* The get_<objecttype>_metadata filter is odd. It's always passed "null" as a value. If
* we return a non-null value then that value is used. If we return null, it will fetch the
* real value. Watch out for that!
*/
add_filter('get_user_metadata', 'prefix_default_meta_order', 10, 3);
function prefix_default_meta_order($value, $user_id, $key) {
$new_value = $value;
if ($key == 'metaboxhidden_post') {
// We don't want this to recurse - remove the filter
remove_filter('get_user_metadata', 'prefix_default_meta_order', 10);
if (!metadata_exists('user', $user_id, $key)) {
// Return value needs to be wrapped in an array as it's expecing a single result.
$new_value = array(array('trackbacksdiv', 'postcustom', 'commentstatusdiv', 'slugdiv', 'authordiv', 'formatdiv'));
}
add_filter('get_user_metadata', 'prefix_default_meta_order', 10, 3);
} else if ($key == 'meta-box-order_post') {
// We don't want this to recurse - remove the filter
remove_filter('get_user_metadata', 'prefix_default_meta_order', 10);
if (!metadata_exists('user', $user_id, $key)) {
$new_order = array(
'advanced' => '',
'side' => 'icl_div,submitdiv,article-typediv,topics-issues,institutiondiv,locationdiv,postimagediv,tagsdiv-post_tag,tagsdiv-temp-other-category',
'normal' => 'postexcerpt,wpseo_meta,article-options,publications,related_items,revisionsdiv,trackbacksdiv,commentsdiv,icl_div_config,postcustom,commentstatusdiv,slugdiv,authordiv,historical-meta',
);
// Return value needs to be wrapped in an array as it's expecing a single result.
$new_value = array($new_order);
}
add_filter('get_user_metadata', 'prefix_default_meta_order', 10, 3);
}
return $new_value;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment