Created
May 4, 2018 18:27
Star
You must be signed in to star a gist
A basic example of a class using actions and filters in WordPress
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 Thing { | |
/** | |
* Get an instance of this class | |
*/ | |
static function get_instance() { | |
static $instance = null; | |
if ( null === $instance ) { | |
$instance = new static(); | |
$instance->setup_actions(); | |
$instance->setup_filters(); | |
} | |
return $instance; | |
} | |
/** | |
* Hook in to WordPress via actions | |
*/ | |
public function setup_actions() { | |
add_action( 'widgets_init', [ $this, 'action_widgets_init' ] ); | |
} | |
/** | |
* Hook in to WordPress via filters | |
*/ | |
public function setup_filters() { | |
add_filter( 'the_content', [ $this, 'filter_the_content' ] ); | |
} | |
public function action_widgets_init() { ... } | |
public function filter_the_content( $content = '' ) { | |
return $content; | |
} | |
} | |
// Kick things off... | |
Thing::get_instance(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment