我想默认启用菜单屏幕选项CSS classes
我应该用什么代码来实现这个目标?
答案 0 :(得分:3)
您可以向get_user_option_{$option}
添加过滤器,其中{$option}
是managenav-menuscolumnshidden
功能中选中的用户元键wp_nav_menu_setup()
。如果元值为false
,这将是用户第一次登录,则会设置默认的隐藏选项。如果你勾选到这个,你可以设置自己的默认值,并确保它永远不会返回false
。
// add a filter to catch unset hidden options
add_filter( 'get_user_option_managenav-menuscolumnshidden', 'my_default_menuscolumnshidden', 99, 3 );
function my_default_menuscolumnshidden( $result, $option, $user_id ){
if( $option == "managenav-menuscolumnshidden" ){
// remove this function to prevent a loop
remove_filter( 'get_user_option_managenav-menuscolumnshidden', 'my_default_menuscolumnshidden', 99 );
// check if it is false (first login)
$result = get_user_option( $option );
if ( false === $result ) {
// update with your own defaults (omits CSS from the hidden options)
$result = array( 0 => 'link-target', 1 => 'xfn', 2 => 'description' );
update_user_option($user->ID, 'managenav-menuscolumnshidden',
$result,
true);
}
// re-add filter in case it gets called again
add_filter( 'get_user_option_managenav-menuscolumnshidden', 'my_default_menuscolumnshidden', 99 );
}
// if this was false before, now it is set to custom defaults
return $result;
}