Add shortcode support.

This commit is contained in:
Gytis Repečka 2020-07-17 16:58:32 +03:00
parent a8291082b5
commit a57e6e7460
Signed by: gytisrepecka
GPG Key ID: BE7648906D706003
3 changed files with 42 additions and 0 deletions

View File

@ -41,6 +41,15 @@ class Plugin_Name_Loader {
*/ */
protected $filters; protected $filters;
/**
* The array of shortcode registered with WordPress.
*
* @since 1.0.0
* @access protected
* @var array $shortcodes The shortcode registered with WordPress to fire when the plugin loads.
*/
protected $shortcodes;
/** /**
* Initialize the collections used to maintain the actions and filters. * Initialize the collections used to maintain the actions and filters.
* *
@ -50,6 +59,7 @@ class Plugin_Name_Loader {
$this->actions = array(); $this->actions = array();
$this->filters = array(); $this->filters = array();
$this->shortcodes = array();
} }
@ -81,6 +91,18 @@ class Plugin_Name_Loader {
$this->filters = $this->add( $this->filters, $hook, $component, $callback, $priority, $accepted_args ); $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, $priority = 10, $accepted_args = 1 ) {
$this->shortcodes = $this->add( $this->shortcodes, $tag, $component, $callback, $priority, $accepted_args );
}
/** /**
* A utility function that is used to register the actions and hooks into a single * A utility function that is used to register the actions and hooks into a single
* collection. * collection.
@ -124,6 +146,10 @@ class Plugin_Name_Loader {
add_action( $hook['hook'], array( $hook['component'], $hook['callback'] ), $hook['priority'], $hook['accepted_args'] ); 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'] ), $hook['priority'], $hook['accepted_args'] );
}
} }
} }

View File

@ -173,6 +173,8 @@ class Plugin_Name {
$this->loader->add_action( 'wp_enqueue_scripts', $plugin_public, 'enqueue_styles' ); $this->loader->add_action( 'wp_enqueue_scripts', $plugin_public, 'enqueue_styles' );
$this->loader->add_action( 'wp_enqueue_scripts', $plugin_public, 'enqueue_scripts' ); $this->loader->add_action( 'wp_enqueue_scripts', $plugin_public, 'enqueue_scripts' );
$this->loader->add_shortcode( 'plugin-name-shortcode', $plugin_public, 'plugin_name_shortcode_func' );
} }
/** /**

View File

@ -100,4 +100,18 @@ class Plugin_Name_Public {
} }
/**
* Shortcode processing function.
* Shortcode can take arguments like [plugin-name-shortcode argm='123']
*/
public function plugin_name_shortcode_func($atts) {
$a = shortcode_atts( array(
'argm' => '0',
), $atts
);
return (
$a['argm']
);
}
} }