1

i'm using code from the wordpress codex: (this is the generic version, my code is working though)

$response = wp_remote_post( $url, array(
    'method' => 'POST',
    'timeout' => 45,
    'redirection' => 5,
    'httpversion' => '1.0',
    'blocking' => true,
    'headers' => array(),
    'body' => array( 'name' => 'bob', 'email' => 'bob@example.com' ),
    'cookies' => array()
    )
);

if ( is_wp_error( $response ) ) {
   $error_message = $response->get_error_message();
   echo "Something went wrong: $error_message";
} else {
   echo 'Response:<pre>';
   print_r( $response );
   echo '</pre>';
}

I'm trying to figure out how to have a form that users can input their name and email address and then it will submit to us to be added to a newsletter.

So basically i want to have the body entries be dynamic

I was thinking a form like this:

<form name="getUserInfo" action="NOT SURE WHAT GOES HERE" method="post">
  <input type="text" name="name" />
  <input type="email" name="email" />
  <input type="submit" name="submit" value="submit"/>
</form>

but i don't know how to send that to the code

i believe it's along this method where i change this line:

'body' => array( 'name' => 'bob', 'email' => 'bob@example.com' ),

to

'body' => array( 'name' => '$namefromform', 'email' => '$emailfromform' ),

i know i'm missing something but i'm not sure.

Generally speaking i'd like this to run as a widget to make my newsletter signup in the footer of the site.

If anyone is wondering why i need an api for this, it is because the company i'm helping create the website for sends all their contacts to a larger 3rd party contact management system.

1

You could make the form action be the current page's URL. When the user submits the form the page gets reloaded with the POST parameters. Then in your widget's code you check if those parameters exist. If so, you check them and then process them with your wp_remote_post.

Personally I use Gravity Forms for stuff like this. I create the form in Gravity and then add code to the gform_after_submission hook to process the form entry. It's an easy option because it takes care of the rest of the form functionality (if you can afford the license).

  • thanks for your input Andreyu. I needed to know how to connect the form to the wp_remote_post. I got it though. Thanks again. – rudtek Jun 15 '16 at 21:56
1

I figured out what I was missing. May have been simple to some but it's worth posting in case someone else has a similar question. I set the post information from the form to variables and then assigned them in wp_remote_post.

I first re-did my form to add specific names to it:

<form name="getUserInfo" action="' . esc_url( $_SERVER['REQUEST_URI'] ) . '" method="post">
  <input type="text" name="name" />
  <input type="email" name="email" />
  <input type="submit" name="submit" value="submit"/>
</form>

then I assigned variables based on the form data.

    $name    = sanitize_text_field( $_POST["rt-name"] );
    $email   = sanitize_email( $_POST["rt-email"] );

They were added in place of the body line in my original question:

'body' => array( 'name' => '$name', 'email' => '$email' ),

Then i added an if/then statement around my wp_remote_post:

 if ( isset( $_POST['rt-submitted'] ) ) {  wp_remote_post here }

so that it only runs if post info from the form is present.

works perfectly.

credit in part goes to this post as well: shortcode doesn't work

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.