Australia's Leading Digital Marketing Experts. T. 1300 235 433  |  Aggreagtion Enquires Welcome

Embed Pastebin Pastes in WordPress with Shortcode (or With a PHP Function)

While the term 'pastebin' has found its place in everyday geek vernacular, it was Pastebin.com that effectively pioneered the service. Created it 2002, Pastebin took nearly 8 years to reach the first million pastes, however, the service now boasts nearly 70 million pastes with over 75 percent marked as private or unlisted. Because Pastebin allows anonymous text (and code) to be posted online it has become a defacto destination for those pasting anonymous news, text, and hacks (for example, during the 2014 Venezuelan protests, Pastebin was blocked by the government as one of the sites where activists were sharing information). The hacker group Anonymous are also known to use the service to paste hacked usernames and passwords. That dark usage aside, the site is an excellent means of quickly sharing information.

Services such as Github (and in particular Gists) have become more popular because of the versioning control and other advanced features that other earlier services often lack. Github is almost certainly a more reliable, feature-rich, and cosmetically appealing location to store more permanent code.

The shortcode on this page will permit you to embed Pastebin snippets to your WordPress website with their JavaScript or iframe embed code. There's also a PHP function for use outside of WordPress.

The Result

Using the shortcode of [pastebin id="https://pastebin.com/vQrmq3HV"] the following paste will be posted into your WordPress post or page.

By default we'll use the JavaScript embed option. You can, however, if the code is extremely long or you don't want it to occupy screen real-estate, use an iframe where you may specify the height. For example, if we were to link to the controversial and long text showcasing YouP*rn's password statistics , we could use [pastebin id="prUbcTmh" js="0" height="300"]. The result:

You should set a default height in the shortcode function to negate the need to define it every time you use the iframe feature.

Shortcode Parameters

id

The id is either the pastebin ID or the full paste URL.

js

The JavaScript embed code is used by default. For longer code you may want to use an iframe to preserve space on your page. Use js="0".

height

If the iframe embed code is used (js="0") you should specify the iframe height as height="400". You can set the default height in the shortcode function.

The 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 Pastebin Pastes in WordPress with Shortcode (or With a PHP Function)
4
 http://www.beliefmedia.com/pastebin-php-wordpress
5
*/
6
 
7
function beliefmedia_pastebin($atts) {
8
  extract(shortcode_atts(array(
9
    'id' => 'vQrmq3HV',
10
    'js' => 1, /* If iframe you must specify a height */
11
    'height' => '200',
12
  ), $atts));
13
 
14
  /* If a URL we'll snatch the ID */
15
  if (strripos($id, 'http') !== false) {
16
   $id = rtrim($id, '/');
17
   $chunks = explode("/", $id);
18
   $id = end($chunks);
19
  }
20
 
21
 /* Return Paste */
22
 if ($js) return '<script src="https://pastebin.com/embed_js.php?i=' . $id . '"></script>';
23
 else return '<iframe src="https://pastebin.com/embed_iframe.php?i=' . $id . '" style="border:none;width:100%;height:' . $height . 'px"></iframe>';
24
}
25
add_shortcode('pastebin','beliefmedia_pastebin');

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.

Function to be used outside WordPress

Used outside WordPress, the following PHP function should be used:

1
<?php 
2
/*
3
 Embed Pastebin Pastes in WordPress with Shortcode (or With a PHP Function)
4
 http://www.beliefmedia.com/pastebin-php-wordpress
5
*/
6
 
7
function beliefmedia_pastebin($id, $js = true, $height = '400') {
8
 
9
  /* If a URL we'll snatch the ID */
10
  if (strripos($id,'http') !== false) {
11
   $id = rtrim($id, '/');
12
   $chunks = explode("/", $id);
13
   $id = end($chunks);
14
  }
15
 
16
  if ($js) $return = '<script src="https://pastebin.com/embed_js.php?i=' . $id . '"></script>';
17
   else $return = '<iframe src="https://pastebin.com/embed_iframe.php?i=' . $id . '" style="border:none;width:100%;height:' . $height . 'px"></iframe>';
18
 
19
 return $return;
20
}
21
 
22
/* Usage with JavaScript embed */
23
$id = "https://pastebin.com/vQrmq3HV";
24
25
 
26
/* Usage with iframe embed (this time with just the ID) */
27
$id = "prUbcTmh";
28
echo beliefmedia_pastebin($id, $js = false);

Download


Title: Embed Pastebin Pastes (WP Plugin)
Description: Embed Pastebin Pastes in WordPress with Shortcode (or With a PHP Function).
  Download • Version 0.2, 1.3K, zip, Category: WordPress Plugins (General)
WordPress Shortcodes, (652.0B)    PHP Code & Snippets, (640.0B)    

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

  AUS Eastern Standard Time (Washington)

  Want to have a chat?
 

Like this article?

Share on facebook
Share on Facebook
Share on twitter
Share on Twitter
Share on linkedin
Share on Linkdin
Share on pinterest
Share on Pinterest

Leave a comment

READY TO HAVE A CHAT? CALL US ANYTIME ON 1300 235 433