I have a subscribe form in wordpress that (currently) only has the ability to add submitted email to a custom post type (subscribers
) and show it in backend.I am wondering how to also have this functionality, and send this email to a custom url (concretely to APSIS).
So I have a form
<form id="subscribe" class="subscribe_form" name="subscribe_form" action="#" method="post">
<input name="subscriber_email" class="subscriber_email" placeholder="Your mail here">
<input class="submit" type="submit" value="Submit">
</form>
My save custom post type function that gets executed via AJAX
<?php
add_action('wp_ajax_save_subscriber', 'save_subscriber');
add_action('wp_ajax_nopriv_save_subscriber', 'save_subscriber');
if (!function_exists('save_subscriber')) {
function save_subscriber() {
if (isset($_POST['subscriber_email']) && is_email($_POST['subscriber_email'])) {
global $wpdb;
$post_data = array(
'post_type' => 'subscribers',
'post_status' => 'publish'
);
$published_id = wp_insert_post( $post_data );
add_post_meta($published_id, 'subscriber_email', $_POST['subscriber_email']);
$out = 'OK';
} else{
$out = 'ERROR';
}
die($out);
}
}
And my AJAX
$('#subscribe').submit(function() {
'use strict';
var str = $(this).serialize() + '&action=save_subscriber';
var $form = $(this);
var $wrapper = $(this).parent();
$.ajax({
type: 'POST',
url: custom.ajaxurl,
data: str,
success: function(msg){
if( msg === 'OK' ) {
$form.animate({ height: '0px' }, 800, function() {
$form.hide();
});
$wrapper.find('.success_message').delay(400).html(custom.success).slideDown(600);
}else {
$wrapper.find('.subscriber_email').addClass('field_error').attr('placeholder', custom.error_mail).val('').focus();
}
}
});
return false;
});
Now I have some more wrappers, and noonce field etc. but that's not important here.
This works fine when you want to just add a post to the CPT, but I need to submit this to
http://www.anpdm.com/public/process-subscription-form.aspx?formId=xxxxxxxxxxx
I got the id and everything from the client, now I need to implement this.
I've seen something about curl, but I've never done anything with it, so I don't really know where to start. Since my action is pointing to my save_subscriber()
function I recon that in that function I'd also have to add a way to send this form to the required url. But how?
Any info will help, thanks.
ANSWER
So following the answer provided by silver, I managed to get it working.
#Run CURL
$url = 'http://www.anpdm.com/public/process-subscription-form.aspx?formId=xxxxxxxxxx';
$request = curl_init();
curl_setopt_array( $request, array (
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_URL => $url,
CURLOPT_POST => 1,
CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13',
CURLOPT_POSTFIELDS => $custom_query_string,
));
$response = curl_exec($request);
$response_data = curl_getinfo($request);
curl_close($request);
#END CURL
if ($response_data['http_code'] == 200) {
$out = 'OK';
}
The formId
is the form number specific for this user, and the $custom_query_string
conformed to the form provided by APSIS and contained something like this:
pf_Email=$_POST['email']&
Submit=Prenumerera&
pf_DeliveryFormat=HTML&
pf_MailinglistName1=xxxxx&
pf_FormType=OptInForm&
pf_OptInMethod=SingleOptInMethod&
pf_CounterDemogrFields=0&
pf_CounterMailinglists=1&
pf_AccountId=xxxx&
pf_ListById=1&
pf_Version=2
After that all worked, I get my subscribers in the wordpress backend, and they appear in the APSIS console where the users are :)
I guess I needed user_agent and correct query string.