Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
WordPress Plugin Skeleton
<?php
/*
Plugin Name: Plugin name
Plugin URI: http://pluginurl.com
Description: Plugin description
Version: 1.0.0
Author: Plugin author
Author URI: http://pluginauthorurl.com
Text Domain: plugin-name
*/
/**
* Plugin version
*/
define( 'PLUGIN_NAME_VERSION', '1.0.0' );
/**
* PluginName class
*
* @since 1.0.0
*/
class PluginName {
/**
* Static instance
*
* @since 1.0.0
*
* @access private
* @var instance
*/
private static $instance;
/**
* Constructor method
*
* @since 1.0.0
*
* @access public
*/
public function __construct() {
// Add Plugin Hooks
add_action( 'plugins_loaded', array( $this, 'add_hooks' ) );
// Load Translation
load_plugin_textdomain( 'plugin-name' );
// Plugin Activation/Deactivation
register_activation_hook( __FILE__, array( $this, 'plugin_activation' ) );
register_deactivation_hook( __FILE__, array( $this, 'plugin_deactivation' ) );
}
/**
* Initializes the plugin object and returns its instance
*
* @since 1.0.0
*
* @access public
* @return object The plugin object instance
*/
public static function get_instance() {
if ( ! isset( self::$instance ) ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Adds all the plugin hooks
*
* @since 1.0.0
*
* @access public
* @return void
*/
public function add_hooks() {
// Actions
add_action( 'init', array( $this, 'init' ) );
}
/**
* Init this plugin
*
* @since 1.0.0
*
* @access public
* @return void
*/
public function init() {}
/**
* What to do when the plugin is being deactivated
*
* @since 1.0.0
*
* @access public
* @param boolean $network_wide
* @return void
*/
public function plugin_activation( $network_wide ) {
if ( is_multisite() && $network_wide ) {
$ms_sites = wp_get_sites();
if( 0 < sizeof( $ms_sites ) ) {
foreach ( $ms_sites as $ms_site ) {
switch_to_blog( $ms_site['blog_id'] );
$this->plugin_activated();
restore_current_blog();
}
}
} else {
$this->plugin_activated();
}
}
/**
* Perform plugin activation tasks
*
* @since 1.0.0
*
* @access private
* @return void
*/
private function plugin_activated() {}
/**
* What to do when the plugin is being activated
*
* @since 1.0.0
*
* @access public
* @param boolean $network_wide
* @return void
*/
public function plugin_deactivation( $network_wide ) {
if ( is_multisite() && $network_wide ) {
$ms_sites = wp_get_sites();
if( 0 < sizeof( $ms_sites ) ) {
foreach ( $ms_sites as $ms_site ) {
switch_to_blog( $ms_site['blog_id'] );
$this->plugin_deactivated();
restore_current_blog();
}
}
} else {
$this->plugin_deactivated();
}
}
/**
* Perform plugin deactivation tasks
*
* @since 1.0.0
*
* @access private
* @return void
*/
private function plugin_deactivated() {}
}
/**
* Init PluginName
*/
PluginName::get_instance();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment