Roots Discourse

Create a Form without plugin

Hello Everyone,

I heard talking about sage and I wanted to try it.
I’m on a new project from scratch. In the contract with my client, I can’t use any plugin.

I need to develop a form. How can I build it correctly ?

For now, it is directly in the template, it works, but I want to use sage to avoid this kind of procedural code.
I would like to put it in a controller, but my $message variable isn’t available anymore.

basically, this is my code in the controller :``

public function checkSubmitForm()
    if (isset($_POST['contact_form'])) {
      // sanitize form values
      $lastname = sanitize_text_field($_POST["lastname"]);
      $firstname = sanitize_text_field($_POST["firstname"]);
      $email = sanitize_email($_POST["email"]);
      $subject = sanitize_text_field($_POST["subject"]);
      $message = esc_textarea($_POST["message"]);
      // get the blog administrator's email address
      $to = get_option('admin_email');
      $headers = "From: $name <$email>" . "\r\n";

      // If email has been process for sending, display a success message
      if ( wp_mail( $to, $subject, $message, $headers ) ) {
        $message = __('Thanks, we will answer the sooner as possible.');
      } else {
        $message = __('An unexpected error occurred');

is it the good way to build it ?
I also see it exists the action “admin_post_no_priv” to build form, but in this case, how we redirect on the right template with an error/success message ?
In my case, it’s just a simple form in the template and it will submit on this same page.


Hello, many views and no answers.
Let me know if my issue is not clear enough.
I would like to build something with the best practices of community.

When you say “can’t use any plugin”, does that mean you can’t use an existing plugin, but can create your own, or does that mean you can’t put any code, yours or others, in /wp-content/plugins/ or /wp-content/mu-plugins/?

Is the “without a plugin” requirement negotiable at all? It seems to me it would take a serious amount of development time to meet even a tiny number of the features of something like Gravity Forms or HTML Forms. A plugin ecosystem that can give you configurable notifications, saving submissions to the database in case of email outages, etc with little or no configuration effort are the reason to use a CMS like WordPress.

No, just can’t use well known plugin or whatever codebase that I didn’t write myself. :slight_smile:

I understand, but that’s the clauses of the project.
Plus, why using a gravity form when you just need to send a simple email plain text.
That’s over engineering. It’s the only form we need to.

Email is fundamentally unreliable. If the client experiences an outage, or the site gets blacklisted, or any number of other moving parts fails, email stops flowing. Saving submissions to the database is a failsafe in these cases. I can say from experience that it’s saved me and my clients countless times.

Good luck with this project.

1 Like

And I agree with you.
Sometimes, the client need to face the trouble to accept some previous advices ^^
I did my job, I adviced them by write. :slight_smile:
But thanks for your time

1 Like

The reason here for using a theme for the form is probably the missing flexibility of existing forms.

There is a plugin for building forms with Gutenberg (using Gutenberg blocks for the form elements),
which is awesome. Sadly it doesn’t seem to be actively developped.

This topic was automatically closed after 42 days. New replies are no longer allowed.