In today’s digital ecosystem, integrating your website with a powerful CRM like Microsoft Dynamics 365 can streamline your business operations—especially when dealing with lead capture, recruitment, or customer support. In this blog, we’ll walk you through how to send form data from a WordPress plugin directly into Microsoft Dynamics CRM using their Web API and OAuth 2.0 authentication.
Why Integrate WordPress with Microsoft Dynamics CRM?
Microsoft Dynamics CRM helps manage customer relationships, track sales, and automate processes. When integrated with WordPress, it allows you to:
- Automatically send form submissions to your CRM.
- Centralize candidate or lead data.
- Eliminate manual data entry.
- Build a seamless recruitment or sales funnel.
Step-by-Step Guide: WordPress to Dynamics CRM Integration
1. Create a Custom Plugin
Start by creating a new folder in your WordPress wp-content/plugins
directory, e.g., /candidate-application-form
. Inside, create a main PHP file (e.g., candidate-application-form.php
) with the following plugin header:
/*
Plugin Name: Candidate Application Form
Description: Sends WordPress form data to Microsoft Dynamics CRM.
Version: 1.0
Author: Your Name
*/
2. Build a Frontend Form with Shortcode
Use a shortcode to render the form, so it can be placed on any page or post. The form includes fields like name, email, resume upload, etc. Example:
add_shortcode('candidate_application_form', function() {
ob_start();
include plugin_dir_path(__FILE__) . 'form.php'; // HTML file with form markup
return ob_get_clean();
});
3. Enqueue Scripts and Styles
Include your form’s CSS and JavaScript using wp_enqueue_scripts
. Also, localize your script to pass the AJAX URL and nonce for secure submission.
function candidate_application_enqueue_scripts() {
wp_enqueue_script('jquery');
wp_enqueue_style('candidate-style', plugin_dir_url(__FILE__) . 'assets/css/style.css');
wp_enqueue_script('candidate-script', plugin_dir_url(__FILE__) . 'assets/js/script.js', array('jquery'), '1.0', true);
wp_localize_script('candidate-script', 'ajax_object', array(
'ajax_url' => admin_url('admin-ajax.php'),
'security' => wp_create_nonce('job-application-form-nonce')
));
}
add_action('wp_enqueue_scripts', 'candidate_application_enqueue_scripts');
4. Handle Form Submission with AJAX
Register an AJAX handler to process the form on submission:
add_action('wp_ajax_submit_application_form', 'submit_application_form');
add_action('wp_ajax_nopriv_submit_application_form', 'submit_application_form');
// Inside submit_application_form()
check_ajax_referer('job-application-form-nonce', 'security');
$name = sanitize_text_field($_POST['name']);
$email = sanitize_email($_POST['email']);
// Add more sanitization and validation
$resume_url = ''; // handle file upload if necessary
$data = [
"fullname" => $name,
"emailaddress" => $email,
"resumeurl" => $resume_url
];
submit_data_to_dynamics_crm($data);
OAuth 2.0 Authentication for Dynamics API
Before you can post data to Microsoft Dynamics CRM, you need to authenticate using OAuth 2.0. Set up your Azure App Registration and grab:
- Client ID
- Client Secret
- Tenant ID
Then, generate the access token:
function get_oauth_token() {
$data = [
'grant_type' => 'client_credentials',
'client_id' => 'your-client-id',
'client_secret' => 'your-client-secret',
'scope' => 'https://your-org.crm.dynamics.com/.default'
];
$token_url = 'https://login.microsoftonline.com/your-tenant-id/oauth2/v2.0/token';
$response = wp_remote_post($token_url, [
'body' => $data
]);
$body = wp_remote_retrieve_body($response);
$result = json_decode($body, true);
return $result['access_token'] ?? false;
}
Send Data to Dynamics CRM
Now that you have the token, construct a payload and send it to your CRM entity (e.g., new_candidates
):
function submit_data_to_dynamics_crm($data) {
$token = get_oauth_token();
if (!$token) return false;
$response = wp_remote_post('https://your-org.api.crm.dynamics.com/api/data/v9.2/new_candidates', [
'headers' => [
'Authorization' => 'Bearer ' . $token,
'Content-Type' => 'application/json'
],
'body' => json_encode($data)
]);
return wp_remote_retrieve_response_code($response) === 204;
}
Resume File Uploads (Optional)
If your form includes file uploads, use wp_insert_attachment()
to upload and store them properly, and send the file URL to Dynamics as part of your data.
// Save the uploaded file and return URL
function handle_resume_upload() {
if (!function_exists('wp_handle_upload')) {
require_once(ABSPATH . 'wp-admin/includes/file.php');
}
$uploadedfile = $_FILES['resume'];
$upload_overrides = ['test_form' => false];
$movefile = wp_handle_upload($uploadedfile, $upload_overrides);
if ($movefile && !isset($movefile['error'])) {
return $movefile['url'];
}
return '';
}
Final Thoughts
Integrating WordPress with Microsoft Dynamics CRM unlocks serious productivity gains. Whether you’re managing job applicants, sales leads, or support inquiries, this connection ensures your CRM stays updated—automatically and securely.
By using a custom plugin, you maintain full control over form fields, design, and CRM mappings. This approach is flexible, scalable, and suitable for agencies or enterprise-level recruitment portals.