add action for displaying posts using a shortcode

I’m developing a simple plugin for letting a user insert a post from the frontend.

I have a shortcode [add-post] which a user can put on any page, say, for eg. My Wall Page

Read More

Now on My Wall Page a simple form comes up which has a title field, a content field and a category select field so that user can choose which category to put his post into.

I’m not sure as what the action should be?

add_shortcode('add-post','my_fucntion');
function my_function(){
  <form>...My form for inserting a new post goes here...</form>
}

add_action('????', 'my_fucntion');

What should the hook be? I tried ‘init’, ‘widgets_init’ but to no avail. If i don’t add any action or even if I do add ‘init’ or ‘widgets_init’, it displays my form 3 times; two times at the top and one time in place of the shortcode (i.e, it replaces my shortcode)

Related posts

Leave a Reply

2 comments

  1. You don’t need to hook into an action for a shortcode – adding the shortcode using add_shortcode() will make it run when you call the_content(). That said, your function for your shortcode needs to return your output, not just echo it outright. Otherwise you will find that your form will appear much earlier than you expect it to.

  2. You don’t need to hook into an action for a shortcode – adding the shortcode using add_shortcode() will make it run when you call the_content(). That said, your function for your shortcode needs to return your output, not just echo it outright. Otherwise you will find that your form will appear much earlier than you expect it to.