1

My first plugin attempt with basic PHP skills and a bunch of WordPress experience... What I want:

User fills out form - form action sends them off to a specialized gateway (got that part ok) Select data from the form is saved to a custom table

This is going to probably sound stupid but i think i have all the elements I am just missing how to put them together... This is a very basic example of what I want to expand on later.

This is the function (the stuff that makes the table etc already happens earlier)

function rchs_add_donor() {
   global $wpdb;
   $welcome_name = $_POST['name'];
   $welcome_text = $_POST['text'];

   $rows_affected = $wpdb->insert( play_donors, 
        array( 
                'time' => current_time('mysql'), 
                'name' => $welcome_name, 
                'text' => $welcome_text ) );
}

Now the silly part - how/where do i put the form to utilize this function?

I tested the above with straightforward data instead of variables from the $_POST and that writes to the new table fine.

If anyone could point me in the right direction that would be great!

Did more research based on the first answer came up with this: but need to make sure it is 'correct' and 'secure' seems like i should be sanitizing data or something before writing it to the dbase?

add_shortcode('donor_form_sc','donor_form');
function donor_form(){
global $wpdb;

echo '<form method="post" action="' . $this_page .'">
  <label for="name" id="name">Name: </label>
<input type="text" name="name" id="name" />
<label for="text" id="text">Text: </label>
<input type="text" name="text" id="text" />

<input type="submit" />
  </form>';

  };
 $welcome_name = $_POST['name'];
   $welcome_text = $_POST['text'];
    $rows_affected = $wpdb->insert( play_donors, 
        array( 
                'time' => current_time('mysql'), 
                'name' => $welcome_name, 
                'text' => $welcome_text ) );

It writes to the dbase as expected.

1 Answer 1

0

Do you mean how you can display the form on your website? If so, write a shortcode for it :) http://codex.wordpress.org/Shortcode_API

Edit: Oh and you might want to add a hook so your function that saves the data is triggered. add_action('init','rchs_add_donor') should do. And add a check in your function to check if your POST is set.

2
  • Ok - added the action... but confused about the rest of it - If I place if (isset($_POST['submit']) { in the function it white screens me. And a shortcode - what I build my form in the shortcode and then call it in my page?
    – Ann
    Commented Jun 12, 2013 at 22:59
  • Be sure to prefix your $_POST variables to prevent another plugin or WordPress itself from trying to process your form variables. Example: $_POST['rchs_submit'], $_POST['rchs_name'], etc. Commented Jun 13, 2013 at 2:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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