Using wp_remote_get to fetch Hubspot posts

One of the clients I work with relies heavily on Hubspot for his marketing needs. Over the last few years we’ve moved all of his posts from his website to Hubspot. In order to accommodate this and receive the fewest errors, we would make a post at both his site and Hubspot and redirect the posts to his Hubspot blog with a script on our single and index file that replaced the domain and redirected automatically.

Recently, we completely removed the blog from his site. Along with creating redirects for any 404 errors that may pop up we were left with how to incorporate his blog into his site. That is where wp_remote_get comes into play.

It took a little while to find the right information but this is what we have come up with.

Using wp_remote_get we can retrieve a json string with our blog feed here is an example:

This returns a json object that we need to decode.

Of this, we also need to decode $hs[‘body’], which is another json string stored inside our $hs array.

We are really close to getting what we need for our post now. Just one last drill down. From the $body array we now have, we need to grab the objects.

Great! We’ve got what need to loop through to create our feed. What’s great is that Hubspot lets us grab pretty much all the data from a post. In our we case we wanted a feed of three posts with featured images, titles, excerpts, permalinks and alt text for the image.

Lastly, we need to create our html. I’m using Bootstrap for quick layout but this can be ignored here and replace with whatever structure you may need.

So here is a quick wrap.

Although in this example I didn’t, I would recommend escaping the variables before output. Also, in the next phase I will save what we need into the wp_options table as a json string, which will speed the site up from always having to retrieve posts on every page load.

I hope this can help anyone out there looking to integrate Hubspot blog posts into a WordPress website.