Skip to content

Instantly share code, notes, and snippets.

@jeffreyvr
Last active March 1, 2017 10:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeffreyvr/71eefde78a9584b0780a00e75be2c6c1 to your computer and use it in GitHub Desktop.
Save jeffreyvr/71eefde78a9584b0780a00e75be2c6c1 to your computer and use it in GitHub Desktop.
Defining priority and arguments to Single Post Meta Manager Loader class.
<?php
/**
* This class is based on the loader class from this tutorial;
* https://code.tutsplus.com/tutorials/object-oriented-programming-in-wordpress-document-the-plugin-ii--cms-21167
*
* In this example I have added the ability to define the priority and arguments for actiond and filters.
*/
class Single_Post_Meta_Manager_Loader {
protected $actions;
protected $filters;
public function __construct() {
$this->actions = array();
$this->filters = array();
}
public function add_action( $hook, $component, $callback, $priority = null, $args = null ) {
$this->actions = $this->add( $this->actions, $hook, $component, $callback, $priority, $args );
}
public function add_filter( $hook, $component, $callback, $priority = null, $args = null ) {
$this->filters = $this->add( $this->filters, $hook, $component, $callback, $priority, $args );
}
private function add( $hooks, $hook, $component, $callback, $priority = null, $args = null ) {
$hooks[] = array(
'hook' => $hook,
'component' => $component,
'callback' => $callback,
'priority' => $priority,
'args' => $args,
);
return $hooks;
}
public function run() {
foreach ( $this->filters as $hook ) {
add_filter( $hook['hook'], array( $hook['component'], $hook['callback'] ), $hook['priority'], $hook['args'] );
}
foreach ( $this->actions as $hook ) {
add_action( $hook['hook'], array( $hook['component'], $hook['callback'] ), $hook['priority'], $hook['args'] );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment