Table of Contents
Introduction: What is Salesforce and How Do I Integrate it with WordPress?
Salesforce is a customer relationship management (CRM) software that helps businesses to manage their customers, leads, and prospects. The CRM system can be integrated with WordPress to provide a seamless experience for the user in terms of utilizing content from both the WordPress platform and the Salesforce platform.
The main advantage in my opinion overall of using Salesforce is that it provides an easy way to manage your customer data from one place. It also integrates with other platforms such as WordPress so you can easily update your website with new information about your company or products without having to switch between multiple screens on your computer or phone if you want to aggregate data from both sources
With the ability to perform HTTP callouts, and the wide accepted REST standard for API communication, we can utilize these to get WordPress data, make posts, and do anything that the WordPress API will allow. I was blown away at the capabilities that the WordPress API had once I figured out the mystique of it.
Salesforce WordPress Integration- What Does the Process Look Like?
That should look familiar as a way to whip up a client that will go request information on your behalf via the HTTP Protocol. For this example “GET” is a relevant method because we will “GET” the posts on a WordPress Blog, but be aware of other methods such as “POST” and “PUT” and others that are common place, just not relevant to this post.
The other thing we need to worry about is Authentication. This is tricky because of how WordPress is setup. Typically a WordPress Blog is a WordPress installation hosted on a server, which in a lot of cases people don’t even have to maintain the server, just log into their site on a shared WordPress host like “HostGator” or something.
Example HTTP callout in Apex
When you integrate WordPress with Salesforce, it is important to understand that Salesforce has to do an HTTP callout in order to interact with the WordPress blog.
The process of doing HTTP callouts in Apex looks like the code below, with only a few of the important values changing like method and endpoint and an optional body:
public class HttpCalloutSample {
// Pass in the endpoint to be used using the string url
public String getCalloutResponseContents(String url) {
// Instantiate a new Http object
Http h = new Http();
// Instantiate a new HTTP request, (GET) the endpoint
HttpRequest req = new HttpRequest();
req.setEndpoint(url);
req.setMethod(‘GET’);
// Send the request, and return a response.
HttpResponse res = h.send(req);
return res.getBody();
}
}
Authentication to WordPress via Salesforce
I found a very simple straight forward way to kickstart an integration for Salesforce and WordPress, which does involve installing a plugin. This is for convenience and in a future post maybe we will explore getting away from this plugin dependency, but in our case it allows for BASIC Authentication for our WordPress Blog to access the REST API.
Basic Authentication
Assuming if you’re hunting down how to code an integration you are familiar with this concept, but for our integration we should recognize the way we are connecting as “Basic Authentication”, which involves a username+password combination being sent with our callout as an “Authorization Header”.
Looking at this code snippet speaks for itself in explaining the setup behind a Basic Authentication:
public static String username = 'username';
public static String password = 'password';
public static Blob headerValue = Blob.valueOf(username + ':' + password);
public static String authorizationHeader = 'Basic ' + EncodingUtil.base64Encode(headerValue);
public static Map<String, String> headers = new Map<String, String>{ 'Authorization' => authorizationHeader };
public static String theSiteURL = 'https://myblog.com/index.php/wp-json/wp/v2/';
-
- Username and Password concatenated with a colon separating them
- The combo text being Blob type
- Sending a header as “Authorization” with “Basic “+ your Blob Base64 Encoded (so a big long string in the end)
These steps are common place in a setting where your security needs are OK with a simpler form of authentication and the access levels are OK to be behind this 1 set of credentials. WordPress has more complex authentication options but this gets us connected.
WordPress Plugin
So I mentioned a plugin to enable the fact that WordPress will allow us to authenticate and have access via this method, and you can find that here: https://github.com/WP-API/Basic-Auth
You can see that it is dead simple, so don’t be deterred by the term plugin. It’s a few lines of code that it adds to the PHP files in your WordPress Installation to enable this Authentication and would be very easy to replicate, but no need to reinvent the wheel. Once this is enabled (AGAIN understand this isn’t a secure Production enterprise solution, this is a proof of concept).
Recap
-
- WordPress plugin provides access via BASIC Authentication method common to integrations
- Once Plugin gives the access to utilize the Auth header our WordPress username and password via the Authentication Header gets us access to the data
- Salesforce HTTP Apex Callouts can utilize these points to do a standard HTTP Callout with the Authorization header to get things like posts, pages, anything that the WordPress REST API supports.
So with that all in place and our ability to suck JSON out of the endpoints WordPress provides, we can focus on working with the data and now doing integration actions with our setup. At the very most basic level, you will start to see that WordPress makes everything available this way, and try going to your WordPress blog and appending wp-json to the URL and watch it spit back to you a public payload of all of your sites content. https://sfdcboss.com/wp-json is an example where you can see that we didn’t even have to authenticate but still can leverage the REST API, but if we went to post we would need to authenticate.
Example Use Cases
I will leave you with a method that can be used to upload media to a blog you have authenticated to, via the above steps. I used it for scraping images, and uploading images into the wordpress blog to host them, and not “hot linking” images. So the code will work with the WordPress REST API via the Authentication mechanism we setup, and send a payload to the Library related URL with the needed information.
wordpress rest api documentation
Closing Thoughts:
Now you know how you can facilitate getting Salesforce and WordPress integration setup to do more than the typical “CMS” functionality Salesforce offers out of box, and it’s up to you whether to make the integration more feature rich or robust. I am interested to hear any use cases that may come out of this though so let me know and good luck.
Resources:
https://gist.github.com/sfboss/ccd3f4169d305e84445583b71356a525
https://developer.wordpress.org/rest-api/