Skip to content

Instantly share code, notes, and snippets.

@deepak-rajpal
Last active September 10, 2015 05:23
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 deepak-rajpal/f8fb397d16bb161b3ba1 to your computer and use it in GitHub Desktop.
Save deepak-rajpal/f8fb397d16bb161b3ba1 to your computer and use it in GitHub Desktop.
WordPress - Sample activation deactivation hook
<?php
class Easy_Newsletter{
// Constructor
function __construct() {
add_action( 'admin_menu', array( $this, 'easy_n_add_menu' ));
register_activation_hook( __FILE__, array( $this, 'easy_n_install' ) );
register_deactivation_hook( __FILE__, array( $this, 'easy_n_uninstall' ) );
}
/*
* Actions perform at loading of admin menu
*/
function wpq_add_menu() {
add_menu_page( 'Easy Newsletter', 'Newsletter', 'manage_options', 'easy-newsletter-dashboard', array(
__CLASS__,
'wpq_page_file_path'
), plugins_url('images/easy-newsletter-logo.png', __FILE__),'2.2.9');
add_submenu_page( 'easy_newsletter_dashboard', 'Easy Newsletter' . ' Dashboard', ' Dashboard', 'manage_options', 'easy_newsletter_dashboard', array(
__CLASS__,
'wpa_page_file_path'
));
add_submenu_page( 'easy_newsletter_dashboard', 'Easy Newsletter' . ' Settings', '<b style="color:#f9845b">Settings</b>', 'manage_options', 'analytify-settings', array(
__CLASS__,
'easy_n_page_file_path'
));
}
/*
* Actions perform on loading of menu pages
*/
function easy_n_page_file_path() {
}
/*
* Actions perform on activation of plugin
*/
$your_db_name = $wpdb->prefix . 'your_db_name';
// function to create the DB / Options / Defaults
function easy_n_uninstall() {
global $wpdb;
global $your_db_name;
// create the ECPT metabox database table
if($wpdb->get_var("show tables like '$your_db_name'") != $your_db_name)
{
$sql = "CREATE TABLE " . $your_db_name . " (
`id` mediumint(9) NOT NULL AUTO_INCREMENT,
`field_1` mediumtext NOT NULL,
`field_2` tinytext NOT NULL,
`field_3` tinytext NOT NULL,
`field_4` tinytext NOT NULL,
UNIQUE KEY id (id)
);";
/* The next step is to actually create the database table. Rather than executing an SQL query directly, we'll use the dbDelta function in wp-admin/includes/upgrade.php (we'll have to load this file, as it is not loaded by default). */
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
}
}
/*
* Actions perform on de-activation of plugin
*/
function easy_n_uninstall() {
}
}
new Easy_Newsletter();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment