WordPress-Plugin-Boilerplate
WordPress-Plugin-Boilerplate copied to clipboard
Add Shortcodes Not Working
How i can add public shortcode.
I read the way of #262 but not works for me. ShortCode adding - and dumping in global $shortcodes of wp - but not parsing.
in main class
private function define_public_hooks() {
$plugin_public = new Zuzas_rating_Public( $this->get_plugin_name(), $this->get_version() );
$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('rating', $plugin_public, 'rating');
}
in public class
public function enqueue_scripts() {
/**
* This function is provided for demonstration purposes only.
*
* An instance of this class should be passed to the run() function
* defined in Zuzas_rating_Loader as all of the hooks are defined
* in that particular class.
*
* The Zuzas_rating_Loader will then create the relationship
* between the defined hooks and the functions defined in this
* class.
*/
wp_enqueue_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'js/zuzas_rating-public.js', array( 'jquery' ), $this->version, false );
}
public function rating($atts, $content = ""){
return 'blabla';
}
in loader
/**
* The array of actions registered with WordPress.
*
* @since 1.0.0
* @access protected
* @var array $shortcodes The actions registered with WordPress to fire when the plugin loads.
*/
protected $shortcodes;
...
public function __construct() {
$this->actions = array();
$this->filters = array();
$this->shortcodes = array();
}
...
/**
* 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 = 2 ) {
$this->shortcodes = $this->add( $this->shortcodes, $tag, $component, $callback, $priority, $accepted_args );
}
...
public function run() {
foreach ( $this->filters as $hook ) {
add_filter( $hook['hook'], array( $hook['component'], $hook['callback'] ), $hook['priority'], $hook['accepted_args'] );
}
foreach ( $this->actions as $hook ) {
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'] ));
}
}
Hey @aios, I just tried out your code in a quick test plugin and its working for me. Not sure why it wouldn't work for you. What you have above is working though.
If you have a full public repo I can look at it.
yes why in loader this library does'not include loader for adding shortcode ?
i fix the shorcodes code by this code :
public function add_shortcode( $tag, $component, $callback, $priority = 10, $accepted_args = 1) {
$this->shortcodes = $this->add( $this->shortcodes, $tag, $component, $callback, $priority, $accepted_args );
}
Thanks for sharing @navotera !