Skip to content

Instantly share code, notes, and snippets.

@Skatox
Created August 23, 2017 14:07
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Skatox/26f50726fcde6b5671988bfe13202494 to your computer and use it in GitHub Desktop.
Save Skatox/26f50726fcde6b5671988bfe13202494 to your computer and use it in GitHub Desktop.
A WordPress plugin class
<?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' ) );
@Skatox
Copy link
Author

Skatox commented Aug 23, 2017

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment