First time creating a WP app, so please bear with me. Essentially, I want to be able to input a value in a form, which gets processed and put into the database. Once that is done, I was trying to use the add_shortcode call inside of a function that handles the entry of new data to the database. However, I found that for some reason add_shortcode simply wouldn't successfully run, unless I put it in the constructor of my class. The problem with that is I need to be able to add shortcodes after the form/data is processed, hence outside the constructor.

Edit: I should also have mentioned that the plugin is using AJAX, and hence the processData method is called whenever a user has input some values in a field. The processData() method is called via an action hook.

I also understand that I would have to account for previous entries saved in a database (this could probably be handled in the constructor, I'd loop through each entry and add the appropriate shortcode). However, how would I go about handling data that has just been entered the database? Assuming the processData() method would be designated for the task, how can I make the call to create a new shortcode?

I have tried a few variations of the add_shortcode() call, but to no avail.

add_action('wp_ajax_processAjax', array('MyPlugin','processData'));

class MyPlugin

//The constructor
public function __construct()
 //Some initialization code

//This function is called with an action_hook
public function processData()
      //process some data

 //This function returns the value for the shortcode
 public function makeShortcode()
      return 'test';


Is there an easy way to be able to add new shortcodes inside the processData function?

  • 1
    your question doesn't entirely make sense. your form isn't going to get submitted on every request, and the shortcode has to be registered on every front end request that it appears on for it to work. – Milo Jan 25 '16 at 0:51
  • I should have mentioned that my plugin is using AJAX, and hence every time the user fills out the form, processData gets called. – mdobrenko Jan 25 '16 at 0:51
  • Your shortcode code won't run, you'll need another piece of code that handles the form processing, that new code will not be in your shortcode – Tom J Nowell Jan 25 '16 at 0:57
  • Hmm, I'm not sure if I'm understanding correctly - but I am using the admin-ajax.php in combination with the add_action statements above my class in order to handle the data. Assuming I know what the input values are, and what I need to pass to the shortcode creation, how would I go about it? – mdobrenko Jan 25 '16 at 1:01
  • Your Shortcode needs to be registered on every request it is to appear on. A Shortcode isn't something you register once and it then exists in perpetuity. If it relies on data in the database, then you have to load whatever you saved in your AJAX action on some previous request and deal with the case where the data may not yet exist. It's two separate pieces that happen in separate requests. – Milo Jan 25 '16 at 1:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.