Читать книгу Professional WordPress Plugin Development - Brad Williams - Страница 56
Uninstall Hook
ОглавлениеWordPress provides an uninstall hook similar to its activation and deactivation hooks outlined earlier in this chapter. If no uninstall.php
file exists for the plugin, WordPress will look for a callback registered via the register_uninstall_hook()
function.
<?php register_uninstall_hook( $file, $callback ); ?>
Parameters:
$file (string, required): The primary plugin file
$callback (callable, required): A function to execute during the uninstall process
Now look at the following example of how a plugin uninstall function works:
<?php register_uninstall_hook( __FILE__, 'pdev_uninstall' ); function pdev_uninstall() { $role = get_role( 'administrator' ); if ( ! empty( $role ) ) { $role->remove_cap( 'pdev_manage' ); } }
That example performs the same action as the uninstall.php
file from the previous section in this chapter. However, you may have noted an important difference. There's no need to check the WP_UNINSTALL_PLUGIN
constant because it's not set in the uninstall callback and unnecessary.
It's also important to use $this
or an object reference when registering an uninstall hook because this callback reference is stored in the database and is unique to the page load when it is stored.