unregister_setting() vs delete_option() for plugin update
-
26-04-2021 - |
Question
I have a plugin (my first major one) and I have some registered settings that are no longer needed. I would like to have these old settings removed from the database to tidy things up and not confuse users with the new settings that will be in the options table.
I set them like this:
register_setting('general_section', 'nav_option_one');
register_setting('general_section', 'nav_option_two');
When I tried to remove them with unregister_setting()
nothing seemed to happen (at least on the DB):
unregister_setting('general_section', 'nav_option_one');
unregister_setting('general_section', 'nav_option_two');
I then when about it with delete_option()
and it removed the row from the DB as expected. However, I am concerned I am missing something.
Can you please let me know what the best method of removing old options settings from the database and what's supposed to happen when using unregister_setting()
is used? I tried looking it up, but no help there: https://developer.wordpress.org/reference/functions/unregister_setting/
Solution
register_setting()
/ unregister_setting()
are used to let WordPress know that it should allow / remove a given setting name—to set up the "allowed options" list, etc. You're telling WordPress that the setting my_name
will exist, and that it is allowed to be used by the Settings API.
add_option()
/ delete_option()
actually do the work of adding / removing the value of the my_name
setting (or option) from the database.
If you've changed the way your plugin sets up its data, you might need to use delete_option()
to remove the data that's already been stored. However, if you're registering different settings, you shouldn't need to use unregister_setting()
on the old ones, since the allowed settings list isn't stored in the database.