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

Uninstall.php

Оглавление

The first method of uninstalling a plugin is by creating an uninstall.php file in the root of your plugin folder. This is the method that you'll most likely be using and is the recommended route to take.

Using uninstall.php is the preferred method of uninstalling a plugin because it isolates your uninstall code from the rest of your plugin and doesn't allow arbitrary code to run from your other plugin files. Ideally, your plugin should be structured such that arbitrary code is never run. However, uninstall.php protects against this and makes sure only the code contained within the file is executed.

It's also important to check that WP_UNINSTALL_PLUGIN is set before executing your uninstall code. Otherwise, it's possible the file will be inadvertently called and your plugin will delete everything when it shouldn't.

In the following example, you can see that the uninstall procedure checks the constant and removes the pdev_manage capability added in the “Plugin Activation Hook” section earlier in this chapter:

<?php if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) { wp_die( sprintf( __( '%s should only be called when uninstalling the plugin.', 'pdev' ), __FILE__ ) ); exit; } $role = get_role( 'administrator' ); if ( ! empty( $role ) ) { $role->remove_cap( 'pdev_manage' ); }

Professional WordPress Plugin Development

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