wp-plugin-starter icon indicating copy to clipboard operation
wp-plugin-starter copied to clipboard

Add docs for shortcode

Open joshcummingsdesign opened this issue 7 years ago β€’ 5 comments

Some frontend code will be powered by shortcodes or will be called in different hooks. Right now the render method is being called in the 'wp_footer' hook. This needs to be clearly documented.

joshcummingsdesign avatar Mar 04 '17 22:03 joshcummingsdesign

Is this why my widget renders output in the bottom of my posts? It doesn't seem to happen on pages, just posts? This doc doesn't exist yet, even in draft form, does it? I don't mind enhancing documentation while I work but I can't cold-write them... I'm new to widgets in WordPress.

FrankV01 avatar Mar 04 '18 14:03 FrankV01

Hi there Josh, I'm new to wp plugin development found your starter kit extremely helpful. Everything works smoothly I'm just struggling with one thing where do I add my "add_shortcode()" code it doesn't seem to work in the main plugin file Thanks!

faizanumer avatar Jan 12 '22 17:01 faizanumer

@FrankV01 @faizanumer Right now I have the render method being called in wp_footer. I've been working on a v3 of this project that will clear up this issue, but here's what I'd suggest in the meantime.

In class-frontend.php create a new method called addShortcodes and put your add_shortcode calls there.

public function addShortcodes() {
  add_shortcode('my_shortcode', function () {
    echo 'Hello World';      
  });
}

Then, use the loader class to hook it to the init action in the define_frontend_hooks method of class-plugin.php like so:

private function define_frontend_hooks() {
  $plugin_frontend = new Frontend($this->plugin_slug, $this->version, $this->option_name);
  $this->loader->add_action('wp_enqueue_scripts', $plugin_frontend, 'assets');
  $this->loader->add_action('wp_footer', $plugin_frontend, 'render');
  $this->loader->add_action('init', $plugin_frontend, 'addShortcodes'); // Add this line
}

There might be better ways of doing this, but this should work for now πŸ‘

Let me know if you have any other questions!

joshcummingsdesign avatar Jan 12 '22 19:01 joshcummingsdesign

Oh man it worked thank you so much for taking the time to reply to this you’re a life saver Many Thanks 😊 Have a great day ahead God Bless!

On Thu, 13 Jan 2022 at 12:24 AM, Josh Cummings @.***> wrote:

@FrankV01 https://github.com/FrankV01 @faizanumer https://github.com/faizanumer Right now I have the render method being called in wp_footer. I've been working on a v3 of this project that will clear up this issue, but here's what I'd suggest in the meantime.

In class-frontend.php create a new method called addShortcodes and put your add_shortcode calls there.

public function addShortcodes() {

add_shortcode('my_shortcode', function () {

echo 'Hello World';

});

}

Then, use the loader class to hook it to the init action in the define_frontend_hooks method of class-plugin.php like so:

private function define_frontend_hooks() {

$plugin_frontend = new Frontend($this->plugin_slug, $this->version, $this->option_name);

$this->loader->add_action('wp_enqueue_scripts', $plugin_frontend, 'assets');

$this->loader->add_action('wp_footer', $plugin_frontend, 'render');

$this->loader->add_action('init', $plugin_frontend, 'addShortcodes'); // Add this line

}

There might be better ways of doing this, but this should work for now πŸ‘

Let me know if you have any other questions!

β€” Reply to this email directly, view it on GitHub https://github.com/joshcummingsdesign/wp-plugin-starter/issues/7#issuecomment-1011377308, or unsubscribe https://github.com/notifications/unsubscribe-auth/AN5EF7YDU74SZJ77XD6IU5TUVXIO7ANCNFSM4DCM5J6Q . You are receiving this because you were mentioned.Message ID: @.***>

faizanumer avatar Jan 12 '22 21:01 faizanumer

Any time 😊

joshcummingsdesign avatar Jan 12 '22 23:01 joshcummingsdesign