Create a Redmine Issue with a WordPress Form

Integration WordPress and Redmine

One of the most powerful features of Redmine is its ability to integrate with other systems, such as your company's website. By integrating Redmine with your website, you can automate the process of creating tasks and issues, saving your team time and energy and accelerating your customer support.

In this article, we will discuss how to integrate Redmine with a website using Gravity Forms in WordPress. Gravity Forms is a popular WordPress plugin that allows users to create forms and collect data from visitors. By using Gravity Forms, you can create tasks in Redmine directly from your website.

Gravity Forms and Redmine API

To create tasks and issues in Redmine using Gravity Forms, 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 task or issue.

// Substitute form id=12 by your form id
add_action( 'gform_after_submission_12', 'send_data_to_redmine', 10, 2 );
function send_data_to_redmine( $entry, $form ) {
    // Get the values of fields id=1,2 & 6 - Substitute by your field ids
    $subject = rgar( $entry, '1' ); // This form value will be the issue subject
    $description = rgar( $entry, '2' ); // This form value will be the issue description
    $custom_field_value = rgar( $entry, '6' ); // This form value will populate an issue custom field
    
    // 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 issue
    $issue_data = json_encode(array(
        'issue' => array(
            'subject' => $subject,
            'description' => $description,
            'project_id' => $project_id,
            'tracker_id' => $tracker_id,
            'status_id' => 1, //change by the status you want the issue to be created in
            'priority_id' => 4, //change by the priority you want the issue to be created in
	'custom_fields' => [
                ['id' => 22, 'value' => $custom_field_value] //change id by the custom field id you want to populate
            ]
        )
    ));
    // Use cURL to send the data to the Redmine API
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $redmine_url . '/issues.json');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $issue_data);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/json',
        'X-Redmine-API-Key: ' . $redmine_api_key
    ));
    $response = curl_exec($ch);
    curl_close($ch);
}

Guidelines

OK, although we have commented the code snippet, let's 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 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.
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 *