Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save robertdevore/cf023ce1604d385ffb497d7dc531abf4 to your computer and use it in GitHub Desktop.
Save robertdevore/cf023ce1604d385ffb497d7dc531abf4 to your computer and use it in GitHub Desktop.
Stop your plugin from being used on websites hosted at wordpress.com
<?php
/**
* Helper function to handle WordPress.com environment checks.
*
* @param string $plugin_slug The plugin slug.
* @param string $learn_more_link The link to more information.
* @return void
*/
function wp_com_plugin_check( $plugin_slug, $learn_more_link ) {
// Check if the site is hosted on WordPress.com.
if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
// Ensure the deactivate_plugins function is available.
if ( ! function_exists( 'deactivate_plugins' ) ) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
// Deactivate the plugin if in the admin area.
if ( is_admin() ) {
deactivate_plugins( $plugin_slug );
// Add a deactivation notice for later display.
add_option( 'my_plugin_deactivation_notice', $learn_more_link );
// Prevent further execution.
return true;
}
}
return false;
}
/**
* Auto-deactivate the plugin if running in an unsupported environment.
*
* @return void
*/
function my_plugin_auto_deactivation() {
if ( wp_com_plugin_check( plugin_basename( __FILE__ ), 'https://example.com/community-statement' ) ) {
return; // Stop execution if deactivated.
}
}
add_action( 'plugins_loaded', 'my_plugin_auto_deactivation' );
/**
* Display an admin notice if the plugin was deactivated due to hosting restrictions.
*
* @return void
*/
function my_plugin_admin_notice() {
$notice_link = get_option( 'my_plugin_deactivation_notice' );
if ( $notice_link ) {
?>
<div class="notice notice-error">
<p>
<?php
echo wp_kses_post(
sprintf(
__( 'My Plugin has been deactivated because it cannot be used on WordPress.com-hosted websites. %s', 'my-plugin' ),
'<a href="' . esc_url( $notice_link ) . '" target="_blank" rel="noopener">' . __( 'Learn more', 'my-plugin' ) . '</a>'
)
);
?>
</p>
</div>
<?php
delete_option( 'my_plugin_deactivation_notice' );
}
}
add_action( 'admin_notices', 'my_plugin_admin_notice' );
/**
* Prevent plugin activation on WordPress.com-hosted sites.
*
* @return void
*/
function my_plugin_activation_check() {
if ( wp_com_plugin_check( plugin_basename( __FILE__ ), 'https://example.com/community-statement' ) ) {
// Display an error message and stop activation.
wp_die(
wp_kses_post(
sprintf(
'<h1>%s</h1><p>%s</p><p><a href="%s" target="_blank" rel="noopener">%s</a></p>',
__( 'Plugin Activation Blocked', 'my-plugin' ),
__( 'This plugin cannot be activated on WordPress.com-hosted websites. It is restricted due to concerns about WordPress.com policies impacting the community.', 'my-plugin' ),
esc_url( 'https://example.com/community-statement' ),
__( 'Learn more', 'my-plugin' )
)
),
esc_html__( 'Plugin Activation Blocked', 'my-plugin' ),
array( 'back_link' => true )
);
}
}
register_activation_hook( __FILE__, 'my_plugin_activation_check' );
/**
* Add a deactivation flag when the plugin is deactivated.
*
* @return void
*/
function my_plugin_deactivation_flag() {
add_option( 'my_plugin_deactivation_notice', 'https://example.com/community-statement' );
}
register_deactivation_hook( __FILE__, 'my_plugin_deactivation_flag' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment