deepak-rajpal
9/9/2015 - 7:50 PM

WordPress - Sample activation deactivation hook

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();
?>