Last active
March 2, 2022 06:45
-
-
Save harisrozak/f3832a4efa32401294c5a9bba4bdf34f to your computer and use it in GitHub Desktop.
Bail out a plugin activation, even before the plugin file is loaded
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Bail out a plugin activation, even before the plugin file is loaded. | |
* Using this hook will prevent any PHP fatal error, because of any defined constants, class names or functions. | |
*/ | |
function bail_out_standalone_plugin_activation( $action, $result ) { | |
$plugin_name = 'YourPluginName'; | |
$plugin_file_name = '/yourpluginname.php'; | |
$activate_plugin_referer = 'activate-plugin_'; | |
$is_activate_plugin_referer = strpos( $action, $activate_plugin_referer ); | |
$is_activated_routes_php = strpos( $action, $plugin_file_name ); | |
if ( false !== $is_activate_plugin_referer && false !== $is_activated_routes_php ) { | |
$plugin_file = str_replace( $activate_plugin_referer, '', $action ); | |
$plugin_file_path = WP_PLUGIN_DIR . '/' . $plugin_file; | |
$plugin_headers = file_exists( $plugin_file_path ) ? get_plugin_data( $plugin_file_path ) : false; | |
if ( $plugin_headers && $plugin_name === $plugin_headers['Name'] ) { | |
wp_redirect( admin_url( 'plugins.php?action=yourpluginname_activation_bail_out' ) ); | |
exit; | |
} | |
} | |
} | |
add_action( 'check_admin_referer', 'bail_out_standalone_plugin_activation', 10, 2 ); | |
/** | |
* Bail out a plugin from a bulk activation, even before the plugin file is loaded. | |
* Using this hook will prevent any PHP fatal error, because of any defined constants, class names or functions. | |
*/ | |
function bail_out_plugin_bulk_activation( $action, $result ) { | |
if ( 'bulk-plugins' !== $action ) return; | |
if ( ! isset( $_POST['action'] ) || 'activate-selected' !== $_POST['action'] ) return; | |
$plugin_name = 'YourPluginName'; | |
$plugin_file_name = '/yourpluginname.php'; | |
$plugins = isset( $_POST['checked'] ) ? (array) wp_unslash( $_POST['checked'] ) : array(); | |
foreach ( $plugins as $key => $plugin ) { | |
if ( false !== strpos( $plugin, $plugin_file_name ) ) { | |
$plugin_headers = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin ); | |
if ( $plugin_name === $plugin_headers['Name'] ) { | |
unset( $_POST['checked'][ $key ] ); | |
} | |
} | |
} | |
} | |
add_action( 'check_admin_referer', 'bail_out_plugin_bulk_activation', 10, 2 ); | |
/** | |
* Bail out a plugin activation message | |
*/ | |
function bail_out_plugin_activation_message() { | |
$action = isset( $_GET['action'] ) ? sanitize_text_field( $_GET['action'] ) : false; | |
if ( 'yourpluginname_activation_bail_out' === $action ) { | |
?> | |
<div class="notice notice-error is-dismissible"> | |
<p><?php _e( 'You just activating the YourPluginName plugin which are not compatible with our new plugin, | |
so we decided to bail out that action.', 'yourpluginname' ); ?></p> | |
</div> | |
<?php | |
} | |
} | |
add_action( 'admin_notices', 'bail_out_plugin_activation_message' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment