-
-
Save pareshsojitra/6b7596ffd5f38bc61dff9e8ac3a48525 to your computer and use it in GitHub Desktop.
Sending form data to an API URL (Postman app helped to test API)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Template Name: Subscribe Page | |
*/ | |
get_header(); | |
if( isset($_POST['subscriber_email']) ){ | |
$api_url = 'http://API_URL'; | |
$subscriber_name = $_POST['subscriber_name']; | |
if ( isset ($_POST['subscriber_surname']) ) { | |
$subscriber_name .= ' '; | |
$subscriber_name .= $_POST['subscriber_surname']; | |
}; | |
$subscriber_zip = $_POST['subscriber_zip_code']; | |
$subscriber_email = $_POST['subscriber_email']; | |
$response = wp_remote_post( $api_url, array( | |
'method' => 'POST', | |
'timeout' => 45, | |
//'redirection' => 5, | |
//'httpversion' => '1.0', | |
'blocking' => true, | |
'headers' => array('content-type' => 'application/json', 'authorization' => 'TOKEN_HERE'), //Token maybe needs some double slash escaping | |
'body' => json_encode(array( | |
'email' => $subscriber_email, | |
'name' => $subscriber_name, | |
'zip_code' => $subscriber_zip | |
) | |
), | |
) | |
); | |
$content_error = ''; | |
//Process server response | |
//If request failed | |
if ( is_wp_error( $response ) ) { | |
$html = '<div id="post-error">'; | |
$html .= __( 'There was a problem retrieving the response from the server.', 'wprp-example' ); | |
$html .= '</div><!-- /#post-error -->'; | |
} | |
else { | |
//If request succedded process messages (There can be error messages from server although request alone is succeeded) | |
$html = '<div id="post-messages">'; | |
$errorMessages=json_decode($response['body']); | |
$message=$errorMessages->message; | |
$status=$errorMessages->success; | |
if ($status){ | |
$html .= '<p class="success">' . __( 'You have successfully subscribed to newsletter') . '</p>'; | |
} | |
else{ | |
$html .= '<p>' . __( 'An error ocurred') . '</p>'; | |
foreach ($message as $messageText) { | |
$html .= '<p class="error">' . $messageText[0] . '</p>'; | |
} | |
} | |
$html .= '</div><!-- /#post-error -->'; | |
} | |
//Replace some unwanted strings from server response | |
$content_error .= str_replace("newsletter subscriptions.", "", $html); ; | |
} | |
?> | |
<?php //Show server response messages | |
if( isset($content_error) ){ echo $content_error; }; ?> | |
<form action="" method="POST"> | |
<div class="form-elements"> | |
<div class="row"> | |
<div class="col-md-4"> | |
<label for="name" class="mandatory">Name</label> | |
<input name="subscriber_name" value="<?php if( isset($_POST['subscriber_name']) ){ echo $_POST['subscriber_name']; }; ?>" type="text" id="name"> | |
</div> | |
<div class="col-md-4"> | |
<label for="surname">Surname</label> | |
<input name="subscriber_surname" value="<?php if( isset($_POST['subscriber_surname']) ){ echo $_POST['subscriber_surname']; }; ?>" type="text" id="surname"> | |
</div> | |
<div class="col-md-4"> | |
<label for="zip_code" class="mandatory">Zip</label> | |
<input name="subscriber_zip_code" value="<?php if( isset($_POST['subscriber_zip_code']) ){ echo $_POST['subscriber_zip_code']; }; ?>" type="text" id="zip_code"> | |
</div> | |
</div> | |
<div class="row"> | |
<div class="col-md-8"> | |
<label for="email" class="mandatory">Email</label> | |
<input name="subscriber_email" value="<?php if( isset($_POST['subscriber_email']) ){ echo $_POST['subscriber_email']; }; ?>" type="text" id="email"> | |
</div> | |
<div class="col-md-4"> | |
<button type="submit" class="cta-button">Submit</button> | |
</div> | |
</div> | |
</div> | |
</form> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment