Select Page

How to use Github Gist in WordPress

by | 22 November 2021 | Wordpress | 0 comments

Why I use Github Gist in WordPress? I often paste a code into my WordPress blog. First I used a plugin Crayon Syntax Highlighter but there is no version fully tested with current version of WordPress. The last tested version of Worpdress is I think 4.2 so it is very old plugin it can’t be use in current time. After a little time I spent for research I decided use just <pre> tag and format it in CSS. But today I decided to use Github Gist in WordPress to show the code, especially when I want to share the code in easy way. The idea of gist is simple: you make a gist and paste the code into this gist. Then you can share the code to your friend by links or you can paste it in your blog. It is very useful when you publish your code in many places, for instance in your personal blog and dev.to, Hashnode or any other blog platform. Then you can paste the code once and if you will change something after some time, you have to change the code only in your gist file. You can attach to the gist one or more files.

So how to use gist in WordPress blog? You should edit your functions.php file (wp-themes/theme-name/functions.php) and attach these code lines:

Then in post you can easy paste the link, but do it without your username!

So let’s assume that this is the link for your gist:

 https://gist.github.com/texe/6da0a676c5c53e3e0b3a48be39b4b6df

You should paste into your post just in line in the place you want to start present your code section, but without your username:

  https://gist.github.com/6da0a676c5c53e3e0b3a48be39b4b6df

Then you will see something like this the first code in this post (functions.php).

So this is it! Now when you publish your post on your personal blog and for instance on dev.to and in the future you will change something in this code you have to make changes only in the gist! After changes every page with link to your gist will show updated code! It’s easy to use Github Gist in WordPress!

 

Follow us