Skip to content

Instantly share code, notes, and snippets.

@harisrozak
Last active March 2, 2022 06:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save harisrozak/f3832a4efa32401294c5a9bba4bdf34f to your computer and use it in GitHub Desktop.
Save harisrozak/f3832a4efa32401294c5a9bba4bdf34f to your computer and use it in GitHub Desktop.
Bail out a plugin activation, even before the plugin file is loaded
<?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