Browse Source

Add shortcode support.

master
Gytis Repečka 1 year ago
parent
commit
a57e6e7460
Signed by: gytisrepecka GPG Key ID: BE7648906D706003
  1. 26
      plugin-name/includes/class-plugin-name-loader.php
  2. 2
      plugin-name/includes/class-plugin-name.php
  3. 14
      plugin-name/public/class-plugin-name-public.php

26
plugin-name/includes/class-plugin-name-loader.php

@ -41,6 +41,15 @@ class Plugin_Name_Loader {
*/
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.
*
@ -50,6 +59,7 @@ class Plugin_Name_Loader {
$this->actions = 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 );
}
/**
* 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
* collection.
@ -124,6 +146,10 @@ class Plugin_Name_Loader {
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'] );
}
}
}

2
plugin-name/includes/class-plugin-name.php

@ -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_scripts' );
$this->loader->add_shortcode( 'plugin-name-shortcode', $plugin_public, 'plugin_name_shortcode_func' );
}
/**

14
plugin-name/public/class-plugin-name-public.php

@ -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']
);
}
}

Loading…
Cancel
Save