Skip to content

Instantly share code, notes, and snippets.

@Ashwinning
Last active November 16, 2016 12:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ashwinning/14f2d028d28eab130afb1872c9021659 to your computer and use it in GitHub Desktop.
Save Ashwinning/14f2d028d28eab130afb1872c9021659 to your computer and use it in GitHub Desktop.
Gist - Wordpress Sync #gistblog

Gist - Wordpress Sync

This is a tale of how this very section - hacks.ash.wine works.

I ❤️ Gists, and I have a habit of posting small stuff/code/discoveries/guides etc. up there so I can revisit them later. (All of my public gists : https://gist.github.com/Ashwinning)

Additionally I use Gistbox, which allows me to categorize/add labels to my gists and find search through them faster.

But not all my gists are important, and most of them aren't useful for other people, so I wanted to archive all the ones that could be potentially useful for others at hacks.ash.wine.

Problems and Solutions

This could have been solved easily by creating a Jekyll repository at Github, and posting stuff there in markdown, but I like being able to have separate Gist repositories for separate things.

That too could be solved by writing a small script to sync my gists with the jekyll, but then

  1. I'd have to port the Wordpress theme/CSS/etc to a jekyll blog.
  2. Redo changes to the theme header & footer every time it changed on the wordpress site.
  3. I wouldn't be able to keep multiple files in their own git repo for each 'hack'.

Also, I've had bad luck with Jekyll (on docker on Windows) before.

The Original Solution

To do this, originally, I created a Python script which would go through all my gists, then all my Wordpress posts, then compare if there were any gists that hadn't been posted to Wordpress yet, and post those.

The script searches for a #gistblog hashtag in the gist description to identify if a gist should be synced, the rest of the hashtags as passed on as Wordpress tags, the remaining gist description is used as the Wordpress post title.

As far as the post content itself was concerned, the script simply posted the gist url as the body of the Wordpress post, and let wordpress handle the embedding.

It worked out pretty well actually! Here's an example of what an embedded post looks like : http://ash.wine/what-a-gist-embed-looks-like/

The Problem

There's a big problem with this approach - SEO.

There's a chance that Google cannot see/will not index the asynchronously loaded content of the gist.

Well, what's the point of aggregating content for others, if other can't find it when they need it?

Introducing Gist-Wordpress-Sync 2.0

The new approach is an iteration over the previous one, but with a couple of new features:

  1. The application now requests the raw content of each tagged gist, runs it though the Github Markdown Parsing API, and posts the rendered HTML content to Wordpress as the post's body.
  2. It stores the gist's id and updated_at attributes as Wordpress custom_fields with every post (to track and update posts).
  3. Abstracts configuration settings into a .settings file so the script can be easily configured for other blogs.

The biggest downside is that since WebHooks don't work for Gists, the application has to be manually run when changes are made to Gists.

It is still very rough around the edges, and may require tweaking for it to fit your taste/preferences, and the configuration of your Wordpress blog.

The repository is available here : https://github.com/Ashwinning/Gist-Wordpress-Sync

If you want to run this for your own blog and need any help, please feel free to open an issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment