Skip to content

Instantly share code, notes, and snippets.

  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save kingkool68/332e2e3aedc5318abf0fcec885b0e45a to your computer and use it in GitHub Desktop.
A basic example of a class using actions and filters in WordPress
<?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