Skip to content

Instantly share code, notes, and snippets.

@danielpataki
Last active July 4, 2021 17:30
Show Gist options
  • Save danielpataki/e851f3adff1b14b261ca to your computer and use it in GitHub Desktop.
Save danielpataki/e851f3adff1b14b261ca to your computer and use it in GitHub Desktop.
Installation And Uninstallation Hooks
register_deactivation_hook( __FILE__, 'my_plugin_deactivation' );
function my_plugin_deactivation() {
// Deactivation rules here
}
register_activation_hook( __FILE__, 'my_plugin_activation' );
function my_plugin_activation() {
add_option( 'my_plugin_activated', time() );
}
add_action( 'init', 'my_custom_post_type' );
function my_custom_post_type() {
$args = array(
'public' => true,
'label' => 'Board Games'
);
register_post_type( 'boardgames', $args );
}
function myplugin_flush_rewrites() {
my_custom_post_type();
flush_rewrite_rules();
}
register_activation_hook( __FILE__, 'myplugin_flush_rewrites' );
global $jal_db_version;
$jal_db_version = '1.0';
function jal_install() {
global $wpdb;
global $jal_db_version;
$table_name = $wpdb->prefix . 'liveshoutbox';
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE $table_name (
id mediumint(9) NOT NULL AUTO_INCREMENT,
time datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
name tinytext NOT NULL,
text text NOT NULL,
url varchar(55) DEFAULT '' NOT NULL,
UNIQUE KEY id (id)
) $charset_collate;";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );
add_option( 'jal_db_version', $jal_db_version );
}
function jal_install_data() {
global $wpdb;
$welcome_name = 'Mr. WordPres';
$welcome_text = 'Congratulations, you just completed the installation!';
$table_name = $wpdb->prefix . 'liveshoutbox';
$wpdb->insert(
$table_name,
array(
'time' => current_time( 'mysql' ),
'name' => $welcome_name,
'text' => $welcome_text,
)
);
}
register_activation_hook( __FILE__, 'jal_install' );
register_activation_hook( __FILE__, 'jal_install_data' );
register_deactivation_hook( __FILE__, 'my_plugin_deactivation' );
function my_plugin_deactivation() {
delete_option('rewrite_rules');
}
register_activation_hook( __FILE__, 'my_plugin_activation' );
function my_plugin_activation() {
global $wp_version;
$php = '5.3';
$wp = '3.8';
if ( version_compare( PHP_VERSION, $php, '<' ) ) {
deactivate_plugins( basename( __FILE__ ) );
wp_die(
'<p>' .
sprintf(
__( 'This plugin can not be activated because it requires a PHP version greater than %1$s. Your PHP version can be updated by your hosting company.', 'my_plugin' ),
$php
)
. '</p> <a href="' . admin_url( 'plugins.php' ) . '">' . __( 'go back', 'my_plugin' ) . '</a>'
);
}
if ( version_compare( $wp_version, $wp, '<' ) ) {
deactivate_plugins( basename( __FILE__ ) );
wp_die(
'<p>' .
sprintf(
__( 'This plugin can not be activated because it requires a WordPress version greater than %1$s. Please go to Dashboard &#9656; Updates to gran the latest version of WordPress .', 'my_plugin' ),
$php
)
. '</p> <a href="' . admin_url( 'plugins.php' ) . '">' . __( 'go back', 'my_plugin' ) . '</a>'
);
}
}
register_activation_hook( __FILE__, 'my_plugin_activation' );
function my_plugin_activation() {
flush_rewrite_rules();
}
add_action( 'init', 'my_custom_post_type' );
function my_custom_post_type() {
$args = array(
'public' => true,
'label' => 'Board Games'
);
register_post_type( 'boardgames', $args );
}
function myplugin_flush_rewrites_deactivate() {
flush_rewrite_rules();
}
register_deactivation_hook( __FILE__, 'myplugin_flush_rewrites_deactivate' );
register_activation_hook( __FILE__, 'my_plugin_activation' );
function my_plugin_activation() {
add_option( 'my_plugin_activation','just-activated' );
}
add_action( 'admin_init','my_plugin_initialize' );
function my_plugin_initialize() {
if( is_admin() && get_option( 'my_plugin_activation' ) == 'just-activated' ) {
delete_option( 'my_plugin_activation' );
flush_rewrite_rules();
}
}
add_action( 'init', 'my_custom_post_type' );
function my_custom_post_type() {
$args = array(
'public' => true,
'label' => 'Board Games'
);
register_post_type( 'boardgames', $args );
}
if ( ! current_user_can( 'activate_plugins' ) ) {
return;
}
check_admin_referer( 'bulk-plugins' );
if ( __FILE__ != WP_UNINSTALL_PLUGIN ) {
return;
}
if ( ! current_user_can( 'activate_plugins' ) ) {
return;
}
$plugin = isset( $_REQUEST['plugin'] ) ? $_REQUEST['plugin'] : '';
check_admin_referer( "deactivate-plugin_{$plugin}" );
register_uninstall_hook( __FILE__, 'my_plugin_uninstall' );
function my_plugin_uninstall() {
// Uninstallation stuff here
}
if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) {
exit;
}
// Uninstallation actions here
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment