allBlogsList

Publishing Content from Sitecore Content Hub to WordPress

Overview

An example Azure Logic App to allow Content Hub publish its content to WordPress

Getting Started

Sitecore Content Hub is a great unified platform, which can serve as a single base for all kinds of enterprise content, including Assets, product catalogs, site pages, social media, and much more. In this post I'll describe how to build and easily integrate Content Hub with WordPress so that when, say, a Product content is added in Content Hub, a new blog post is automatically created in WordPress, or, to put it differently, Content Hub would publish content to 3rd party platform (WordPress). This approach can really be applied to any kind of content and any kind of 3rd party publishing destination, as long as it provides an API to publish content to it.

I wrote a couple of blog posts, describing how this approach can be used to publish changes from Content Hub to Apache Solr and Sitecore OrderCloud. Here I'd like to how simple it is to extend this approach to a different publishing destination, this time, WordPress.

Again, here I'm using the Azure Logic App, which uses a combination of custom and prebuilt Azure functions to get transform Content Hub content and get it posted to WordPress REST API. I'm using my custom function named GetEntityData to read the Content Hub entity, the source code for it can be found here.

The below screenshot is taken from the Azure logic App designer for my sample indexer. I like this very visual way of coding apps - easy to understand and easy to maintain. This app is POC to illustrate the idea, not quite production-ready, but it works and can easily be improved with Azure queues, retry and poison message handling logic, enhanced logging with App insights, and so on, but that would make this blog post very long :) publish This Logic App will extract an Entity ID for the published entity, read its data via Content Hub's REST API to send relevant product information to the "WordPress, Create Post" function, which is already available on the Azure portal. I've done this for illustration purposes, just to illustrate an idea, so this is just POC, but this works.

Details on Azure Functions

When a HTTP request is received

publish The first item is an entry point of this entire Azure Logic App, It's an abstraction, defining entry point URL and payload JSON schema, which is what Content Hub's "API call" action will invoke each time when target entity is created or deleted (more on this in sections below). An ID of target Entity from Content Hub is getting passed in the payload of this request – it will be used to retrieve that Entity in functions/steps below.

Get Content Hub Entity Data

publish The next function is a custom one: it makes an API call to Content Hub REST API to read the target entity, as well as all related entities, listed in the EntityRelations header parameter. The TargetEntityIdJsonPath parameter is a JSON Path expression, pointing to where the actual ID of the target entity can be found in payload JSON. For the purpose of this POC, I simply passed Content Hub credentials via function header parameters – in real-life production scenarios such information should be stored in configs or, even better, Azure Key Vault.

Internally this function will read all properties from target entities, then read and add all fields from specified related entities and finally append renditions of the target entity. The output is serialized into JSON, which looks like this:

{
 "Properties": { /* target entity properties */},
 "Properties": { /* a collection of elements, holding properties of the related entities, specified in EntityRelations parameter above */},
 "Renditions": { /* collection of rendition names and their Urls in Content Hub */}
} 

See additional sections below for more details on function code and link to code project in GitHub

Parse JSON

This is a built-in Azure Function, which allows extracting fields from provided JSON, in this case, I am parsing the output of my GetEntityData function. Parse JSON requires schema parameter, which can be generated from sample JSON publish

Create Post

Here's my super-simple blog post template for a new product publish This is a WordPress Create Post function, available in Azure – just search for "WordPress"… publish then select it and then choose "Create Post" publish You'll need to log into the target account then select Site ID and provide Title and Content for the new blog, here's my simple example: publish Note the tokens - those are the outputs of the Parse Json function above publish

Response

publish The Last element is to format and send a response to the caller. I simply forward the response body from the above call to OrderCloud since calling the action in Content Hub doesn't really care about the response, but this response might be useful for debugging purposes.

…oh, and here's the new blog post on WordPress :): https://syatsenko.wordpress.com/2021/12/15/powerful-bar-chocolate-pack/


Appendix

Source code for above custom Azure functions and template Azure Logic App: https://github.com/sergyatsenko/SY.ContentHub.AzureFunctions

Triggers and Actions Content Hub Actions and Triggers to invoke a REST API call, such as the above Logic App, are described in Sitecore documentation.