0

I am writting a simple newsletter subscriber plugin. I created a widget with form:

echo '<form action="'.home_url().'" method="post" name="subscriber_form">';
echo '<p><input type="email" name="subscriber_mail" value="" placeholder="'.__('Enter your email','my-text-domain').'" required /></p>';
echo '<input type="submit" name="subscriber_send" value="'.__('Subscribe','my-text-domain').'" class="Buttons">';
echo '</form>';

I could specify the action directly to the file to handle the form but I need wordpress to be loaded.

Is there a hook (like with ajax) to point the form to or is there a way to correctly start the instance of wordpress inside the plugin?

Thanks for feedback.

  • I'm confused at what you're asking, are you asking how to process the form or how to process the form via ajax? If it's a small form you could just look for $_POST in the same script, no need to specify an action then. – Howdy_McGee Aug 29 '14 at 14:54
4

admin_url( 'admin-post.php' ) is the non-ajax version of admin_url( 'admin-ajax.php' )

Similar to admin-ajax.php it will fire one of four hooks:

Logged out users

  • admin_post_nopriv_{action} ($_REQUEST['action'] specified)
  • admin_post_nopriv (no $_REQUEST['action'] specified)

Logged in user

  • admin_post_{action} ($_REQUEST['action'] specified)
  • admin_post (no $_REQUEST['action'] specified)

The 'action' can therefer bespecified as part of the target url (e.g. admin_url( 'admin-ajax.php?action=my-action' )) or as posted along with all the other data.

The disadvantage to this is that if you need to return the user to the form (e.g. because of an error), you will need to redirect them, and so the browser will loose the data they entered. That's terrible UX, so I rarely use this.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.