4

I am using a 3rd party service that does a scrub of my database for new entries within a specific table. However I am been unable to find a simple form plugin that submits data to the wordpress database inside of a table that won't affect any of the wordpress functionality. The reason is my hosting provider only allows me one database.

I need to use a form in my wordpress that:

  1. Captures name, phone, email submitted by end user
  2. Save to the database
  3. Redirect user to confirmation page.

Nothing elaborate really and although it would be great to see the results in the wordpress administration it is not necessarily required since I can just use myphpadmin.

In summary:

I need a simple html/php form, where I can just paste the form html onto a page via the wordpress administration via the html/code view for pages. When a user submits the form, it saves it to the wordpress database and then redirects the user to a "thank you" page.

5
  • Sorry, I can't tell exactly what you want. Can you add some detail?
    – s_ha_dum
    Sep 12, 2013 at 17:39
  • @s_ha_dum I edited my post, hopefully it clarifies it a bit. All I need is a form processor that saves to the wordpress database. So basically, place html code on any page, user submits data, it saves to wordpress database in a unique table.
    – Damainman
    Sep 12, 2013 at 18:03
  • if you consider saving them on wordpress table. Try this tutorial It integrates with contact form 7 plugin
    – Sisir
    Sep 12, 2013 at 18:15
  • Kaiser, I haven't tried anything so far. Most form plugins either generated specific looking forms or emailed the results and I wanted something that saved to the database with the ability to customize the form to look as I choose. Sisir, thank you for the suggestion, I will look into it.
    – Damainman
    Sep 12, 2013 at 18:32
  • Here you can find tutorial to create simple newsletter signup form. i guess this is very small, you can learn $wpdb to make it work for you, kvcodes.com/2016/02/simple-subscribe-form-wordpress
    – Kvvaradha
    Feb 7, 2016 at 6:22

2 Answers 2

6

For what I can understand you already have the table in your database.

I don't know how you have named it, but a best practise (for me a must-do practise) is to name it with the same table prefix of wordpress, that's the one setted in wp-config.php.

You also don't say how this table is structured, but I guess it's somethimg like:

ID (integer,primary,autoincrement) | name (varchar) | phone (varchar) | email (varchar)

You can add a shortcode that print the form. In your functions.php add:

add_action('init', function() {
  add_shortcode('userform', 'print_user_form');
});

function print_user_form() {
  echo '<form method="POST">';
  wp_nonce_field('user_info', 'user_info_nonce', true, true);
  ?>

  All your form inputs (name, email, phone) goes here.  

<?php
  submit_button('Send Data');
  echo '</form>';
}

Now just create a post or a page in wp dashboard and simply add [userform]: the form is magically printend in the page.

As you can see I've not added the action attribute to form, in this way the form send post data to same page.

Now you have to save data. add an action on a early hook, look for the $_POST, check the nonce and save your data:

add_action('template_redirect', function() {
   if ( ( is_single() || is_page() ) &&
        isset($_POST[user_info_nonce]) &&
        wp_verify_nonce($_POST[user_info_nonce], 'user_info')
    ) {
      // you should do the validation before save data in db.
      // I will not write the validation function, is out of scope of this answer
      $pass_validation = validate_user_data($_POST);
      if ( $pass_validation ) {
        $data = array(
          'name' => $_POST['name'],
          'email' => $_POST['email'],
          'phone' => $_POST['phone'],
        );
        global $wpdb;
        // if you have followed my suggestion to name your table using wordpress prefix
        $table_name = $wpdb->prefix . 'my_custom_table';
        // next line will insert the data
        $wpdb->insert($table_name, $data, '%s'); 
        // if you want to retrieve the ID value for the just inserted row use
        $rowid = $wpdb->insert_id;
        // after we insert we have to redirect user
        // I sugest you to cretae another page and title it "Thank You"
        // if you do so:
        $redirect_page = get_page_by_title('Thank You') ? : get_queried_object();
        // previous line if page titled 'Thank You' is not found set the current page
        // as the redirection page. Next line get the url of redirect page:
        $redirect_url = get_permalink( $redirect_page );
        // now redirect
        wp_safe_redirect( $redirect_url );
        // and stop php
        exit();
      }
   }
});

The code is rough, but should be a valid starting point. Inline comments should help you to understand the workflow.

Be sure to read the documents:

5
  • Thank you for the extensively detailed response. That is a lot to digest, I will keep you posted on my results and thank you again.
    – Damainman
    Sep 12, 2013 at 18:33
  • Quick questions: 1. If I needed more than one form is there a way I can add a name to the shortcode such as [userform:FormA] or [userform:Contact] which would add like FormA to the ID class of the html form field as well as put FormA into a hidden field on the form? 2. For your second code block, would that also be put into the functions.php file? 3. What part of the second code example makes it so wordpress knows to only execute that functionality on forms created using this method? I don't see a way of it checking the form submission it came from the form created via the shortcode.
    – Damainman
    Sep 12, 2013 at 19:32
  • (1) what you need is the shortcode atts using them you can customize form as you want, see add_shortcode docs linked in the answer. (2) Yes. Code that start with add_filter or add_action you have to create a plugin to contain it or put in functions.php: depends if you want/can lost that feature when switch theme. (3) the wp_verify_nonce reason to be is just that. It checks a $_POST variable (created by wp_nonce_field) that contain an encripted value created starting from a plain string, in this case 'user_info'. Again see the docs links at the end of answer. @Damainman
    – gmazzap
    Sep 12, 2013 at 20:17
  • quick question, you have a closing php tag at the top and an open php tag towards the bottom of your shortcode function. Can you verify why it is setup that way?
    – Damainman
    Sep 12, 2013 at 21:23
  • @Damainman "All your form inputs (name, email, phone) goes here." is intented to be a placeholder for the html stuff of the form, so I close the php tags before if and reopen after it to avoid a lot and ugly succession of echo. Just write html there as you usually do.
    – gmazzap
    Sep 12, 2013 at 21:35
1

One thing to take into account, NAME is a reserved wordpress word, if you use a field called name, when you submit your form, you will obtain a 404 not found error.

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