-
Notifications
You must be signed in to change notification settings - Fork 2.3k
How To Add Shortcodes
Go to wp-content/plugins/plugin-name/public/class-plugin-name-public.php
and add your shortcode name.
public function paynamics_donation_func( $atts ){
// your Code here..
}
next open wp-content/plugins/plugin-name/includes/class-plugin-name-loader.php
and find the public function __construct()
and add this code $this->shortcodes = array();
. so from this
/**
* Initialize the collections used to maintain the actions and filters.
*
* @since 1.0.0
*/
public function __construct() {
$this->actions = array();
$this->filters = array();
}
to this
/**
* Initialize the collections used to maintain the actions and filters.
*
* @since 1.0.0
*/
protected $shortcodes;
public function __construct() {
$this->actions = array();
$this->filters = array();
$this->shortcodes = array();
}
then find public function run()
and add this at the bottom foreach ( $this->shortcodes as $hook ) { add_shortcode( $hook['hook'], array( $hook['component'], $hook['callback'] )); }
so this will be look like this
/**
* Register the filters and actions with WordPress.
*
* @since 1.0.0
*/
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'] ));
}
}
Still, at class-plugin-name-loader.php
add this code at the bottom.
/**
* 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 );
}
Finally, you need to add a line to includes/class-plugin-name.php
, in function define_public_hooks()
$this->loader->add_shortcode( 'your-shortcode-name', $plugin_public, 'your_shortcode_func' );
In the example at the top of this page 'your_shortcode_func' is called 'paynamics_donation_func'