Why is remove_setting and remove_control not working?

In the WP admin customizer, none of the previous controls are showing,
so although the options have been removed, the values are still
stored, which should have been deleted.

You haven’t done anything to delete them. The code in your question will remove the registered controls from the customiser, and the registered settings, but you need to consider what that means.

When you add a setting to the Customiser with add_setting() what you’re doing is registering various behaviours around saving that setting. Things like controlling which users have permission to save it, and how the setting values should be sanitised.

So when you remove a setting from the Customiser, you are just removing those behaviours, and deregistering any controls that interact with that setting. It’s the opposite of add_setting(), and you’ll note that add_setting() does not save anything to the database.

This is all about the behaviour of the Customiser interface itself. None of this has anything to do with the actual data that’s saved in the database. Once that data’s saved in the database it’s there until you delete it. The get_theme_mod() function does not interact with the Customiser in any way. It simply retrieves the value from the database directly. This is why get_theme_mod() does not return any default value set with add_setting(), and it’s why it’s still returning values for unregistered settings.

If you want to remove a saved theme mod, you need to use remove_theme_mod():

remove_theme_mod( 'psychic2016_logo' );

But note you will only need to run this code once and the value will be permanently deleted (The customize_register hook runs every time the Customiser is loaded).

Leave a Comment