我可以强制主题吗?我的意思是,用户不能有意或无意更改我为该网站制作的主题。我想从核心执行此操作,因为我还想锁定所有更新。
强制主题或不允许主题更改
2 个回复
SO网友:Bryan Willis
这篇帖子上有很多无用的评论,这似乎是一个很简单的问题。
更改如下$theme_directory_name = \'twentyfourteen\';
到您的主题。。。这就是你所要做的。第一个函数可以设置主题。第二个选项禁用编辑主题的界面。
function selfish_hardcoded_theme() {
$theme_directory_name = \'twentyfourteen\';
add_filter( \'template\', create_function( \'$template\', \'return "\' . $theme_directory_name . \'";\' ) );
add_filter( \'stylesheet\', create_function( \'$stylesheet\', \'return "\' . $theme_directory_name . \'";\' ) );
}
add_action( \'setup_theme\', \'selfish_hardcoded_theme\', -1 );
add_filter( \'map_meta_cap\', function( $required_caps, $cap ) {
if ( \'edit_themes\' == $cap || \'switch_themes\' == $cap || \'install_themes\' == $cap || \'delete_themes\' == $cap || \'update_themes\' == $cap ) {
$required_caps[] = \'do_not_allow\';
}
return $required_caps;
}, 10, 2 );
还有其他方法:$theme = wp_get_theme();
if (\'twentyfourteen\' !== $theme->name || \'twentyfourteen\' !== $theme->parent_theme) {
switch_theme( \'twentyfourteen\' );
}
您还可以在wp配置中设置默认主题常量。最后,从管理中删除主题菜单:
function remove_themes_submenus() {
global $submenu;
unset($submenu[\'themes.php\'][5]); // Removes \'Themes\'
unset($submenu[\'themes.php\'][15]); // Removes Theme Installer tab
}
add_action(\'admin_menu\', \'remove_themes_submenus\');
SO网友:Brad Dalton
您可以创建自定义管理角色并删除这些标准管理功能:
function remove_admin_capabilities() {
$admin = get_role( \'administrator\' );
$caps = array(
\'install_themes\',
\'update_themes\',
\'delete_themes\',
\'edit_themes\',
\'switch_themes\',
\'edit_theme_options\',
);
foreach ( $caps as $cap ) {
$admin->remove_cap( $cap );
}
}
add_action( \'init\', \'remove_admin_capabilities\' );
或者简单地强制他们使用编辑器角色。结束