admin管理员组文章数量:1122846
Is it possible to have any control over the screen options for Appearance>Menus
. I am working on a theme and with a fresh Wordpress install where I had never touched any of the screen options ( at least not under Appearance>Menus anyway ). I had 3 custom post types. Of these one was "missing". I investigated Screen Options and it turned out that one was unchecked, while the other two were checked. I don't know what I did to get it this way but I would like it to be consistent for users anyway. Has anyone got any ideas or information on this front? Ideally I would like to know that all three custom post types would be checked on a fresh install or a new user.
Is it possible to have any control over the screen options for Appearance>Menus
. I am working on a theme and with a fresh Wordpress install where I had never touched any of the screen options ( at least not under Appearance>Menus anyway ). I had 3 custom post types. Of these one was "missing". I investigated Screen Options and it turned out that one was unchecked, while the other two were checked. I don't know what I did to get it this way but I would like it to be consistent for users anyway. Has anyone got any ideas or information on this front? Ideally I would like to know that all three custom post types would be checked on a fresh install or a new user.
2 Answers
Reset to default 5This option is stored in the wp_usermeta
table with the meta_key
name of metaboxhidden_nav-menus
.
If we hide all the boxes, this is the meta_value
of the option:
array(
"nav-menu-theme-locations",
"add-custom-links",
"add-post",
"add-page",
"add-portfolio",
"add-category",
"add-post_tag",
"add-post_format",
"add-location"
)
There is one CPT, portfolio
. If we wanted for it to be always visible every time the user visits the page (/wp-admin/nav-menus.php
), this code would do that:
add_filter( 'get_user_option_metaboxhidden_nav-menus', 'cpt_always_visible_wpse_87882', 10, 3 );
function cpt_always_visible_wpse_87882( $result, $option, $user )
{
if( in_array( 'add-portfolio', $result ) )
$result = array_diff( $result, array( 'add-portfolio' ) );
//$show_boxes = array( 'cpt1', 'cpt2', 'cpt3' );
//if( in_array( $show_boxes, $result ) )
// $result = array_diff( $result, $show_boxes );
return $result;
}
But this forces the CPT to be visible even if the user un-checks the Screen Option. In the next visit to the page, the box will be visible again.
To do it one time after user registration, the action hook user_register
should be used together with the function update_user_meta
.
To do it on a fresh install, a custom install.php
could be used. Maybe another technique is available, but not sure about it...
In addition to always displaying a box, I also wanted to always hide a box too. I added onto @brasofilo's answer above to always hide a metabox:
add_filter('get_user_option_metaboxhidden_nav-menus', 'cpt_always_visible_wpse_87882', 11, 3);
function cpt_always_visible_wpse_87882($result, $option, $user) {
// Always make the metabox visible.
if (in_array('add-portfolio', $result)) {
$result = array_diff($result, ['add-portfolio']);
}
// Always hide the metabox(s).
$hide_options = ['add-category', 'add-post-type-post'];
if (!in_array($hide_options, $result)) {
$result = array_merge($result, $hide_options);
}
return $result;
}
本文标签: theme developmentSet default screen option for appearance gt menus
版权声明:本文标题:theme development - Set default screen option for appearance -> menus 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736305603a1932647.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论