Created
August 23, 2017 14:07
-
-
Save Skatox/26f50726fcde6b5671988bfe13202494 to your computer and use it in GitHub Desktop.
A WordPress plugin class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Class description | |
* | |
* @package Plugin name | |
*/ | |
/** | |
* Class My_Class | |
* | |
* @package Plugin name | |
*/ | |
class My_Class | |
{ | |
/** | |
* Class instance, used to control plugin's action from a third party plugin | |
* | |
* @var $instance | |
*/ | |
public static $instance; | |
/** | |
* Adds all the plugin's hooks | |
*/ | |
public static function init() { | |
$self = self::instance(); | |
if ( is_admin() ) { | |
// Put any admin's hooks and filters in here | |
} else { | |
// Put any frontend's hooks and filters in here | |
} | |
} | |
/** | |
* Access to a class instance | |
* | |
* @return My_Class | |
*/ | |
public static function instance() { | |
if ( is_null( self::$instance ) ) { | |
self::$instance = new self; | |
} | |
return self::$instance; | |
} | |
} | |
} // End class. | |
add_action( 'plugins_loaded', array( 'My_Class', 'init' ) ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you need to add a hook/filter you can do it like this (at init() method) :
add_action( 'save_post', array( $self, 'my_save_method' ) );
So it will be attached using the static $self variable and can be modified by third party plugins.