From 3b79c7bf294ab1fd427dc0036b1c604c75f8bddf Mon Sep 17 00:00:00 2001 From: Garrett Hunter Date: Sun, 25 Feb 2018 20:06:24 -0800 Subject: [PATCH] Added shortcode support Started with Devin's code from https://github.com/DevinVinson/WordPress-Plugin-Boilerplate/issues/262#issuecomment-89438555 --- .../includes/class-plugin-name-loader.php | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/plugin-name/includes/class-plugin-name-loader.php b/plugin-name/includes/class-plugin-name-loader.php index 634ec4757..de865b375 100644 --- a/plugin-name/includes/class-plugin-name-loader.php +++ b/plugin-name/includes/class-plugin-name-loader.php @@ -50,6 +50,7 @@ public function __construct() { $this->actions = array(); $this->filters = array(); + $this->shortcodes = array(); } @@ -81,6 +82,18 @@ public function add_filter( $hook, $component, $callback, $priority = 10, $accep $this->filters = $this->add( $this->filters, $hook, $component, $callback, $priority, $accepted_args ); } + /** + * Add a new shortcode to the collection to be registered with WordPress + * + * @since 1.0.0 + * @param string $tag The name of the new shortcode. + * @param object $component A reference to the instance of the object on which the shortcode is defined. + * @param string $callback The name of the function that defines the shortcode. + */ + public function add_shortcode( $tag, $component, $callback) { + $this->shortcodes = $this->add( $this->shortcodes, $tag, $component, $callback); + } + /** * A utility function that is used to register the actions and hooks into a single * collection. @@ -95,7 +108,7 @@ public function add_filter( $hook, $component, $callback, $priority = 10, $accep * @param int $accepted_args The number of arguments that should be passed to the $callback. * @return array The collection of actions and filters registered with WordPress. */ - private function add( $hooks, $hook, $component, $callback, $priority, $accepted_args ) { + private function add( $hooks, $hook, $component, $callback, $priority = 10, $accepted_args = 2 ) { $hooks[] = array( 'hook' => $hook, @@ -124,6 +137,11 @@ public function run() { add_action( $hook['hook'], array( $hook['component'], $hook['callback'] ), $hook['priority'], $hook['accepted_args'] ); } + foreach ( $this->shortcodes as $hook ) { + add_shortcode( $hook['hook'], array( $hook['component'], $hook['callback'] ) ); + + } + } }