WordPress

WordPress: Handling Plugin Activation

A simple way to handle WordPress plugin activation.


function activate_background_slideshow() {
	// do something during activation
	$plugin_data = get_plugin_data(WP_PLUGIN_DIR . '/background-slideshow/background-slideshow.php',false,false);
    
    // WordPress Version
    $version = $plugin_data['Version'];
    if ( version_compare( get_bloginfo( 'version' ), '3.1', '<' ) )
    {
      wp_die("You must use at least WordPress 4.0 to use this plugin!");
    }

    // Plugin Version
    if ( get_option( 'dk_op_version' ) === false )
    {
      // initial state
      add_option( 'dk_op_version', $version );
    }
    else if(get_option( 'dk_op_version') < $version)
    {
      update_option( 'dk_op_version', $version );
      // do update based on version
    }

    // Database version
    if( get_option('dk_op_db') === false ) {
      add_option('dk_op_db', $version);
     
    } else if( get_option('dk_op_db') != $version ) {
      // add more upgrade database if needed
      update_option( 'dk_op_db', $version );
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

16 − 12 =