0

I´m trying to create a plugin for WordPress to collect data from a website job-application form and generate a .XML file for each appliance ... I´m learning php. In this little code presented below, I´m just using 2 fields (Firstname and Lastname) but for the real-case, it would be more fields (numbers and letters)

Problem Statement:

The clients website contained a form, where users would enter job-application. After submitting the form, the HR department would get a notification mail that a new job was submitted.

As the HR department has its own internal HR-system for job-tracking, they needed a solution that would automatically take every new user-submitted application and translate it to XML, in order for the in-house system import it. Here's what I found in the internet to solve the problem:

<?php

 // add the action 
add_action('wpcf7_before_send_mail', 'my_get_form_values');

function my_get_form_values($contact_form) {

    // get info about the form and current submission instance
    $formTitle = $contact_form->title();
    $submission = WPCF7_Submission::get_instance();
    // define which form you're looking for (if you have multiple) 
    // and if the submission is valid
    if ( $formTitle == "myFormName" && $submission ) {
        // get the actual data!
        $posted_data = $submission->get_posted_data();
        $firstName = posted_data['firstname'];
        $lastName = posted_data['lastname'];
    }
}

function my_generate_xml($posted_data) {
    // Get a serial number from the contact form,
    // to distinguish report from others
    $serialNr = $posted_data['SerialNumber'];

    // Create xml doc spec
    $xmlDoc = new DOMDocument('1.0', 'UTF-8');
    $xmlDoc->formatOutput = true;

    // Build Maximizer XML file
    $xmlRoot = $xmlDoc->createElement('Bug report - ticket #' . $serialNr);
    $xmlDoc->appendChild($xmlRoot);

    // Example node for current user

    $xml_User = $xmlDoc->createElement('User');

    $xml_fn = $xmlDoc->createElement('firstname', $posted_data['firstname']);
    $xml_User->appendChild($xml_fn);

    $xml_ln = $xmlDoc->createElement('lastname', $posted_data['lastname']);
    $xml_User->appendChild($xml_ln);

    $xmlRoot->appendChild($xml_User);

    // save it as a file for further processing
    $content = chunk_split(base64_encode($xmlDoc->saveXML()));
    $xmlDoc->save('ticket-'.$serialNr.'.xml');
}

?>

The idea is to each time a user submits the form, we extract the raw data from the submission and generate an XML file. In this case, where the file is created? in the root directory of your WordPress installation? Also a unique ID (from the serial number) would ensures that multiple users can submit the form at the same time and not write into one file.

The plugin was installed and activated, however it´s not working as intented to be.

Any clue what I´m missing?

1 Answer 1

0

The file is not created, because the function my_generate_xml exists, but is never called.

All that code looks correct. The only problem is that you have to call the function that generates the XML file...

add_action('wpcf7_before_send_mail', 'my_get_form_values');

function my_get_form_values($contact_form) {

    // get info about the form and current submission instance
    $formTitle = $contact_form->title();
    $submission = WPCF7_Submission::get_instance();
    // define which form you're looking for (if you have multiple) 
    // and if the submission is valid
    if ( $formTitle == "myFormName" && $submission ) {
        // get the actual data!
        $posted_data = $submission->get_posted_data();

        my_generate_xml( $posted_data );

        /* You don't need these two lines, so you can delete them
        $firstName = posted_data['firstname'];
        $lastName = posted_data['lastname'];
        */
    }
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.