-1

I have been searching for a solution to export WP user data to another application once a form is submitted. There is an article on how to do this but I am too noob to to get it to work and am asking for someone to help me finish the script.

Basically this is an add_action hooks that triggers when a form is submitted. When submitted the script creates an array of the users data and sends it to the application where a form exists to receive the data..

What I am trying to figure out is how do I actually build the array? I have used do_action hooks before so I am ok there and the communication side of the script is fully formed. So, all that is left (at this point) is to insert the array.

I'm very interested an getting this to work and eager to learn what needs to be done.

The script itself can be found here

<?php
/**
 * Push data to a Mautic form
 *
 * @param  array   $data   The data submitted by your form
 * @param  integer $formId Mautic Form ID
 * @param  string  $ip     IP address of the lead
 * @return boolean
 */
function pushMauticForm($data, $formId, $ip = null)
{
    // Get IP from $_SERVER
    if (!$ip) {
        $ipHolders = array(
            'HTTP_CLIENT_IP',
            'HTTP_X_FORWARDED_FOR',
            'HTTP_X_FORWARDED',
            'HTTP_X_CLUSTER_CLIENT_IP',
            'HTTP_FORWARDED_FOR',
            'HTTP_FORWARDED',
            'REMOTE_ADDR'
        );
        foreach ($ipHolders as $key) {
            if (!empty($_SERVER[$key])) {
                $ip = $_SERVER[$key];
                if (strpos($ip, ',') !== false) {
                    // Multiple IPs are present so use the last IP which should be the most reliable IP that last connected to the proxy
                    $ips = explode(',', $ip);
                    array_walk($ips, create_function('&$val', '$val = trim($val);'));
                    $ip = end($ips);
                }
                $ip = trim($ip);
                break;
            }
        }
    }

    $data['formId'] = $formId;
    // return has to be part of the form data array
    if (!isset($data['return'])) {
        $data['return'] = 'http://where-to-redirect.com';
    }
    $data = array('mauticform' => $data);
    // Change [path-to-mautic] to URL where your Mautic is
    $formUrl =  'http://[path-to-mautic]/form/submit?formId=' . $formId;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $formUrl);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("X-Forwarded-For: $ip"));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($ch);
    curl_close($ch);
    return $response;     
}

Now the author says :

pushMauticForm($_REQUEST, 3);

    // $_REQUEST can look like:
    // array('email' => 'test@email.com', 'message' => 'Test message')

Your Mautic Form with ID = 3 then has to have fields called email and message.`

So what I have tried (but doesn't work) is this:

This is form information:

    if ( ! defined( 'ABSPATH' ) ) {
        exit; // Exit if accessed directly
    }

    $user               = get_user_by( 'id', get_current_user_id() );
    $firstname          = ! empty( $_POST['vendor-owner-firstname'] )   ? sanitize_text_field( $_POST['vendor-owner-firstname'] ) : '';
    $lastname           = ! empty( $_POST['vendor-owner-lastname'] )    ? sanitize_text_field( $_POST['vendor-owner-lastname'] ) : '';


    <?php

    /**
     * Push data to a Mautic form
     *
     * @param  array   $data   The data submitted by your form
     * @param  integer $formId Mautic Form ID
     * @param  string  $ip     IP address of the lead
     * @return boolean
     */

    //do_action hook passes 2 arguments $vendorId, $userId 

    add_action('yith_after_form_submitted', "pushMauticForm, 2, 10)";

    function pushMauticForm($vendorId, $userId, $data, $formId, $ip = null)
    {
         //set Mautic form ID
         $formId = '3';

     //get array of form data; 
     //example: match mautic label (left) with form label ($_REQUEST['vendor-owner-firstname']

    global $data;

        $first_name = $data->($_REQUEST['vendor-owner-firstname']);
        $last_name = $data->($_REQUEST['vendor-owner-lastname']);
        $email = $data->($_REQUEST['vendor-email']);
        $some_other_data = $data->($REQUEST['more_data']);
0

I have no way to test this, but the structure looks like so.

function pushMauticForm( $vendorId, $userId, $data, $formId, $ip = null ) { 
    //get array of form data;
    //example: match mautic label (left) with form label ($_REQUEST['vendor-owner-firstname']

    // parse the request args and merge with our defaults
    // this ensures we don't have to do a bunch of isset checks.

    $args = wp_parse_args( $_REQUEST, array (
        'vendor-owner-firstname' => '', 
        'vendor-owner-lastname'  => '',
        'vendor-email'           => '',
        'more_data'              => '',
    ) );

    // load up the data

    $data[ 'firstname' ]  = $args[ 'vendor-owner-firstname' ];
    $data[ 'last_name' ]  = $args[ 'vendor-owner-lastname' ];
    $data[ 'email' ]      = $args[ 'vendor-email' ];
    $data[ 'more_data' ]  = $args[ 'more_data' ];

    //set Mautic form ID

    $formId = '3';
    $data[ 'formId' ] = $formId;

    // return has to be part of the form data array
    if ( ! isset( $data[ 'return' ] ) ) {
        $data[ 'return' ] = 'http://where-to-redirect.com';
    }

    // set the payload
    // NOTE: It's confusing using 2 different $data vars... but $data is a new thing now

    $data = array ( 'mauticform' => $data );

    // Change [path-to-mautic] to URL where your Mautic is
    $formUrl = 'http://[path-to-mautic]/form/submit?formId=' . $formId;
    $ch      = curl_init();
    curl_setopt( $ch, CURLOPT_URL, $formUrl );
    curl_setopt( $ch, CURLOPT_POST, 1 );
    curl_setopt( $ch, CURLOPT_POSTFIELDS, http_build_query( $data ) );
    curl_setopt( $ch, CURLOPT_HTTPHEADER, array ( "X-Forwarded-For: $ip" ) );
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
    $response = curl_exec( $ch );
    curl_close( $ch );
    return $response;
}
  • Thanks @jgraup I will test this i=out this weekend and give you a reply. – user2197724 Mar 4 '16 at 20:36
  • 1
    Perfect! @jgraup Thank you for helping out here as this was really bugging me. simplifying the function certainly helps! :) I appreciate the help...tremendously! – user2197724 Mar 5 '16 at 0:51

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.