RBA Cash Rate: 4.35% · 1AUD = 0.67 USD · Inflation: 4.1%  
Leading Digital Marketing Experts | 1300 235 433 | Aggregation Enquires Welcome | Book Appointment
Example Interest Rates: Home Loan Variable: 5.20% (5.24%*) • Home Loan Fixed: 5.48% (6.24%*) • Fixed: 5.48% (6.24%*) • Variable: 5.20% (5.24%*) • Investment IO: 5.78% (6.81%*) • Investment PI: 5.49% (6.32%*)

Embed Github Gists With PHP (or in WordPress with Shortcode)

Embed Github Gists With PHP (or in WordPress with Shortcode)

GitHub is a Web-based Git repository hosting service that offers the typical distributed revision control and source code management functionality of Git, as well as some of its own features. The site has excelled with the the web-based graphical interface, desktop, and mobile integration, in addition to offering the traditional command line tools. The service provides excellent access control, collaboration features (such as bug tracking, feature requests, task management, and wikis for every project).

GitHub also operates a "pastebin-style" service called Gist that is used for hosting code snippets. The service builds upon the simple and traditional pastebin-style repositories littering the web by adding version control for code and easy forking. Because each "gist" has its own Git repository, multiple code snippets can be contained in a single paste and they can be pushed and pulled using Git. Further, forked code can be pushed back to the original author in the form of a patch, so gists (the Github term for the more traditional pastes) can become more like mini-projects.

Github has a number of subscription options of which the most basic is free. Anonymous code can be pasted, but the code cannot be deleted once created.

This article will provide you with very simple shortcode that will embed Github Gists into your WordPress post or page with shortcode. A PHP function to be used outside of WordPress is also provided.

The Result

The shortcode of either [gist id="https://gist.github.com/martinkhoury/dcaf0b7f2d76231d8c6d"] or just [gist id="dcaf0b7f2d76231d8c6d"] will return the following code (an older version of the code shared in this article). View the (secret) snippet on Github here .

Shortcode Function

Copy and paste the WordPress function into your theme's functions.php file or, if you sensibly have one installed, your custom functions plugin. You may optionally download and install our plugin from the bottom of of the page.

1
<?php 
2
/*
3
 Embed Github Gists With PHP (or in WordPress with Shortcode)
4
 http://www.beliefmedia.com/github-php-wordpress
5
*/
6
 
7
function beliefmedia_wordpress_github($atts) {
8
  extract(shortcode_atts(array(
9
    'id' => '' /* Gist URL or ID */
10
  ), $atts));
11
 
12
  /* If a URL we'll snatch the ID */
13
  if (strripos($id,'http') !== false) {
14
   $id = rtrim($id, '/');
15
   $chunks = explode(&quot;/&quot;, $id);
16
   $id = end($chunks);
17
  }
18
 
19
 return '<p><script src=&quot;https://gist.github.com/' . $id . '.js&quot;></script></p>';
20
}
21
add_shortcode('gist','beliefmedia_wordpress_github');

If you require shortcode to work in a sidebar widget, you'll have to enable the functionality with a filter. If you're using our custom functions plugin, you'll have that feature enabled by default.

Shortcode Attributes

id

The ID can be either the full Gist URL or just the Gist ID. For example, you may use [gist id="https://gist.github.com/martinkhoury/98689469889fc449eba8"] or just [gist id="98689469889fc449eba8"].

PHP Function

Used outside of WordPress, the following function can be used.

1
<?php 
2
/*
3
 Embed Github Gists With PHP (or in WordPress with Shortcode)
4
 http://www.beliefmedia.com/github-php-wordpress
5
*/
6
 
7
function beliefmedia_github($id) {
8
  /* If a URL we'll snatch the ID */
9
  if (strripos($id,'http') !== false) {
10
   $id = rtrim($id, '/');
11
   $chunks = explode(&quot;/&quot;, $id);
12
   $id = end($chunks);
13
  }
14
 return '<p><script src=&quot;https://gist.github.com/' . $id . '.js&quot;></script></p>';
15
}
16
 
17
/* Usage */
18
$id = 'https://gist.github.com/martinkhoury/dcaf0b7f2d76231d8c6d';
19
echo beliefmedia_github($id);

Considerations

The shortcode is extremely simple. However, embed code should be avoided when pasting anything into a website - shortcode is almost always a better idea. If the embed code format changes, of you choose to alter formatting, a simple alteration in your code effects changes on your entire website.

While I have no plans of activity on Github, you can follow me on Github here .

Download


Title: Embed Github Gists With PHP (WordPress Plugin)
Description: Embed Github Gists With PHP (or in WordPress with Shortcode).
  Download • Version 0.2, 1.4K, zip, Category: WordPress Plugins (General)
WordPress Shortcodes, (529.0B)    PHP Code & Snippets, (506.0B)    

Download our 650-page guide on Finance Marketing. We'll show you exactly how we generate Billions in volume for our clients.

  E. Australia Standard Time [ UTC+10, Default ] [ CHECK TO CHANGE ]

  Want to have a chat?
 

RELATED READING

Like this article?

Share on Facebook
Share on Twitter
Share on Linkdin
Share on Pinterest

Leave a comment