Last active
September 15, 2024 15:06
-
-
Save shaunpalmer/2180e58500540264dba79635c6083819 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
/** | |
* Plugin Name: Your Plugin Name | |
* Description: Your plugin description. | |
* Version: 1.0.0 | |
* Author: Your Name | |
* License: GPL-2.0+ | |
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt | |
* | |
* @package YourPluginNamespace | |
*/ | |
// Exit if accessed directly. | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; | |
} | |
// Define plugin constants for activation/deactivation hooks. | |
define( 'YOUR_PREFIX_ON_ACTIVATION', 'your_prefix_on_activation' ); | |
define( 'YOUR_PREFIX_ON_DEACTIVATION', 'your_prefix_on_deactivation' ); | |
// Ensure the required WP file for plugin functions is included. | |
if ( ! function_exists( 'is_plugin_active' ) ) { | |
require_once ABSPATH . 'wp-admin/includes/plugin.php'; | |
} | |
/** | |
* Activation hook callback. | |
*/ | |
function your_prefix_activate_plugin() { | |
if ( current_user_can( 'activate_plugins' ) ) { | |
add_option( YOUR_PREFIX_ON_ACTIVATION, 'yes' ); | |
do_action( YOUR_PREFIX_ON_ACTIVATION ); | |
} | |
} | |
register_activation_hook( __FILE__, 'your_prefix_activate_plugin' ); | |
/** | |
* Deactivation hook callback. | |
*/ | |
function your_prefix_deactivate_plugin() { | |
if ( current_user_can( 'activate_plugins' ) ) { | |
do_action( YOUR_PREFIX_ON_DEACTIVATION ); | |
delete_option( YOUR_PREFIX_ON_ACTIVATION ); | |
} | |
} | |
register_deactivation_hook( __FILE__, 'your_prefix_deactivate_plugin' ); | |
/** | |
* Handle actions after plugin activation. | |
*/ | |
function your_prefix_handle_activation() { | |
if ( is_admin() && 'yes' === get_option( YOUR_PREFIX_ON_ACTIVATION ) ) { | |
delete_option( YOUR_PREFIX_ON_ACTIVATION ); | |
do_action( YOUR_PREFIX_ON_ACTIVATION ); | |
} | |
} | |
add_action( 'admin_init', 'your_prefix_handle_activation' ); | |
/** | |
* Register actions for logging plugin activation and deactivation events. | |
*/ | |
function your_prefix_register_log_actions() { | |
add_action( | |
YOUR_PREFIX_ON_ACTIVATION, | |
function() { | |
your_prefix_log_event( 'activated' ); | |
} | |
); | |
add_action( | |
YOUR_PREFIX_ON_DEACTIVATION, | |
function() { | |
your_prefix_log_event( 'deactivated' ); | |
} | |
); | |
} | |
add_action( 'plugins_loaded', 'your_prefix_register_log_actions' ); | |
/** | |
* Log plugin events if WP_DEBUG is true. | |
* | |
* @param string $event The event to log (e.g., 'activated', 'deactivated'). | |
*/ | |
function your_prefix_log_event( $event ) { | |
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { | |
error_log( sprintf( 'Plugin %s: %s', $event, __FILE__ ) ); | |
} | |
} | |
/** | |
* Example of another hook triggered by activation. | |
*/ | |
function your_prefix_init_action() { | |
if ( did_action( YOUR_PREFIX_ON_ACTIVATION ) ) { | |
// Additional functionality or setup code on plugin activation. | |
} | |
} | |
add_action( 'init', 'your_prefix_init_action' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment