Create a Redmine Helpdesk Ticket with a WordPress Form

WordPress and Redmine Helpdesk Integration

In my previous post I described the process of how to create a Redmine issue from your WordPress website, using Gravity Forms.

Today, we will go a step forward by reviewing how to integrate your WordPress website with the famous Redmine UP Helpdesk plugin. We want to create a helpdesk ticket with the data we

collect from the Gravity Form in WordPress.

NOTE: if you use Redmine and want to extend its functionality to a whole helpdesk system for internal or external users, then you have to check Redmine UP's Helpdesk plugin here.

Gravity Forms and Redmine UP API

To create tasks and issues in Redmine using Gravity Forms and additionally include the dedicated data for the helpdesk ticket you will need to use a code snippet like the one provided below. This code snippet uses the Gravity Forms API to retrieve the values of certain fields from your form and then sends them to the Redmine API to create a new ticket. The Redmine UP's Helpdesk API is also involved. In this case we will not use the JSON method, but an xml file.

// Substitute form id=109 by your form id
add_action( 'gform_after_submission_109', 'send_data_to_helpdesk', 10, 2 );
function send_data_to_helpdesk( $entry, $form ) {
    // Get the values of fields 1, 3, 4, 5 and 6 - Substitute by your field ids
    $contact_name = rgar( $entry, '1' );
    $contact_email = rgar( $entry, '3' );
    $subject = rgar( $entry, '4' );
    $description = rgar( $entry, '5' );
    $custom_field_value = rgar( $entry, '6' );
    
    // URL of your Redmine instance
    $redmine_url = 'https://hereyourredmineurl.com';
    
    // Your Redmine API key (substitute by your user's API; more info below this code snippet)
    $redmine_api_key = '12345678901234567890';
    
    // Tracker ID for the issue
    $tracker_id = 4; //Redmine tracker to be created
    
    // Project ID for the issue
    $project_id = 18; //Redmine project that will contain the issue
    // Prepare the data for the ticket
    $ticket_data = '<?xml version="1.0"?>
    <ticket>
      <issue>
        <project_id>' . $project_id . '</project_id>
        <tracker_id>' . $tracker_id . '</tracker_id>
	<author_id>1</author_id>
	<subject>' . $subject . '</subject>
        <description>' . $description . '</description>
            <status_id>1</status_id> //change by the status you want the issue to be created in
            <priority_id>4</priority_id> //change by the priority you want the issue to be created in
            <assigned_to_id>1</assigned_to_id> //change by the user id you want the issue to be assigned to
        <custom_fields type="array">
          <custom_field id="75">
            <value>' . $custom_field_value . '</value>
          </custom_field>
        </custom_fields>
      </issue>
      <contact>
        <email>' . $contact_email . '</email>
        <first_name>' . $contact_name . '</first_name>
      </contact>
    </ticket>';

    // Use cURL to send the data to the Redmine Helpdesk API
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $redmine_url . '/helpdesk/create_ticket.xml');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $ticket_data);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/xml',
        'X-Redmine-API-Key: ' . $redmine_api_key
    ));
    $response = curl_exec($ch);
    $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    // Check if the API call was successful
    if($httpcode != 201) {
        // The API call failed, log the error and return
        error_log("Failed to create Redmine UP's Helpdesk ticket. HTTP code: " . $httpcode . ", Response: " . $response);
        return;
    }
    // The API call was successful, continue with other actions if needed
}

Guidelines

OK, although the code snippet includes explanatory comments, I will give some guidelines for its use:

  • substitute the ids indicated in the snippet by your trackers, fields, etc, id
  • you can get the Gravity Form's id in the forms list and you can get the form field ids by checking each field's settings.
  • to get your Redmine project's id you will have to follow these steps:
    • go to this page: https://hereyourredmineurl.com/projects.xml (you will probably need to be logged in to Redmine as administrator)
    • look for the details of the project you want the issue to be created in
    • annotate the “id”
  • to get trackers', priorities' or fields' ids, just go to Administration and hover the cursor over the different entities. In the URL that will indicate the browser, you will see the id for each value.
  • substitute the fake API code of the snippet by the right API value. To get the API key, please follow these steps:
    • chose which Redmine user will be the author of the tickets created from the form. You could also create a generic user for this purpose.
    • The user has to log in to Redmine, go to the “My Account”. In the left sidebar you will see the API key feature. Create a new one and copy the result.
  • After the ticket is created in Redmine all feeds (like Redmine notifications or automatic acknowledgement of receipt to the customer will be triggered as a normal Redmine Helpdesk ticket.
IMPORTANT: the user that will be the author of the issues created from the form has to be a member of the project were the issue will be created. She has to have the create, edit and view permissions for her role in the project.
WHERE SHOULD I PLACE THE CODE SNIPPET? This PHP code snippet should be placed in your WordPress website's theme's function.php file. However I recommend using the “Code snippets” plugin, which is much more easy and safe to manage. Remember to activate the code snippet after its creation.

About The Author

Leave a Comment

Your email address will not be published. Required fields are marked *