Читать книгу Professional WordPress Plugin Development - Brad Williams - Страница 104

Removing Settings

Оглавление

As a professional plugin developer, it's important to create a high‐quality experience for your users, which includes tasks that are rarely noticed. In this case you're going to learn how to remove settings that your plugin has created when it is uninstalled in WordPress.

You're going to use register_uninstall_hook(), which was covered in Chapter 2, “Plugin Framework.” First you need to register your uninstall function as shown here:

register_uninstall_hook( __FILE__, 'pdev_plugin_uninstall' );

Next you'll use the unregister_setting() function of the Settings API. The unregister_setting()function accepts the following parameters:

 option_group: Settings group name used during registration

 option_name: Name of the option to unregister

You can simply grab these two values from when you registered your settings group. Let's look at the full function in action.

// Deregister our settings group and delete all options function pdev_plugin_uninstall() { // Clean de-registration of registered setting unregister_setting( 'pdev_plugin_options', 'pdev_plugin_options' ); // Remove saved options from the database delete_option( 'pdev_plugin_options' ); }

The unregister_setting() function will unregister your settings group from WordPress. You also need to remove the value from the database using delete_option(). Remember you are using the uninstall hook, which means this code will be executed only if the plugin is uninstalled. If the plugin is deactivated, the options will remain and be available should the user decide to reactivate the plugin.

Professional WordPress Plugin Development

Подняться наверх