Saturday, January 15, 2022

[FIXED] How to save new registrations to another database hosted in the same hosting site

Issue

I am working on a wordpress site and I need to save the form registrations from the main site at the same time to the subdomain database. I'm using godaddy as the hosting service and contact form 7 for the forms.

Can someone help me? Thank you so much


Solution

This is how you get Contact Form 7 request

add_filter( 'wpcf7_mail_components', 'show_cf7_request', 10, 3 );
function show_cf7_request( $components, $wpcf7_get_current_contact_form, $instance ) { 
    post_to_another_url('https://site-to.post', $_REQUEST);
    return $components; 
}; 

and forward id to another url as JSON POST

function post_to_another_url($url, $data {
    if(is_array($data) && $url != '') {
        // copied from => https://tecadmin.net/post-json-data-php-curl/
        $payload = json_encode($data);

        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLINFO_HEADER_OUT, true);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);

        // Set HTTP Header for POST request 
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json',
            'Content-Length: ' . strlen($payload))
        );

        // Submit the POST request
        $result = curl_exec($ch);

        // Close cURL session handle
        curl_close($ch);
    }
}

if you want insert data in same database, but different table, use $wpdb->insert() https://developer.wordpress.org/reference/classes/wpdb/insert/



Answered By - Beneris

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.