How to Disable Theme Switching in WordPress

In WordPress, changing a theme is as easy as clicking a button. This is great in terms of flexibility and usability, but this is not so great when you accidentally switch your theme and break the site functionality.

This is especially true if you are managing your WordPress site of client(s) or if you have a multi-author blog with more than one admin. In case you don’t know, a site theme in WordPress is much more that the looks of a site. A theme may host several different features like custom widgets, shortcodes, etc.

So when you accidentally switch or change a theme, you might lose the functionality and all the widgets you’ve placed carefully may get lost or misplaced. Definitely, this will not be the first thing you want to deal with in the morning.

In this quick post, let me show you how to disable theme switching in WordPress.

Disable Theme Switching in WordPress

It is very easy to disable theme switching in WordPress. To start off, open your favorite FTP client and then log in. Once you are logged in, open the functions.php file in your theme directory.

disable-theme-switching-functions-file

After opening the functions file, copy and paste the below code in it and then save the file. Since the code snippet below doesn’t belong to the theme, you can also place it in the site-specific plugin.

//Disable Theme Switching
function bs_disable_theme_switching() {
	global $submenu;
	unset($submenu['themes.php'][5]);
	unset($submenu['themes.php'][15]);
}
add_action('admin_init', 'bs_disable_theme_switching');

As soon as you are done adding the code, the submenu item “Themes” will be removed from the “Appearance” menu. Since the users cannot access the Themes page, they cannot easily mess with themes.

disable-theme-switching-theme-page-removed

However, if you want the admin to have the access to the Themes page, then use the following code-snippet. The below code ensures that all every users except admin are blocked from accessing the Themes page.

//Disable theme switching if not admin
function bs_disbale_theme_switching() {
  global $menuitem, $userdata;
  get_currentuserinfo();
  if ($userdata->ID != 1) {
    unset($menuitem['themes.php'][5]);
    unset($menuitem['themes.php'][15]);
  }
}
add_action('admin_init', 'bs_disbale_theme_switching');

If you ever want to revert back, simply remove the added code snippet and you are good to go. If you like this article then you might also want to know how to disable theme and plugin editor in WordPress, it is a best security practice.

That’s all there is to do and it is that simple to disable theme switching in WordPress.

Do comment below sharing your thoughts and experiences about using the above method to disable theme switching in WordPress.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.