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 a CodePen Pen in WordPress with Shortcode

Embed a CodePen Pen in WordPress with Shortcode

CodePen describes their platform as a "playground for the front end side of the web. It's all about inspiration, education, and sharing". The platform takes HTML, CSS, and JavaScript, and runs the code serverside for a result displayed in your browser. It's a great way of finding bugs, showcasing your work, learning, making yourself feel inferior... or just browsing the brilliance of others. CodePen is more addictive than YouTube and more inspiring than Wikipedia - it's awesome.

This quick article will provide you with WordPress shortcode that'll render a Pen into your post or page. Using a shortcode rather than the default embed code makes sense for a number of reasons, not the least of which is that a shortcode makes the returned result more adaptable to the inevitable changes likely to take place in the future.

The Result

Using Tearable Cloth for our example, the shortcode of [codepen id="http://codepen.io/dissimulate/pen/KrAwx" name="Tearable Cloth"] will return the following:

See the Pen Tearable Cloth by dissimulate (@dissimulate) on CodePen.

The id (URL) and name of the Pen is required. While the height can be altered, the width cannot (without additional formatting).

The Shortcode

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 a CodePen Pen in WordPress with Shortcode
4
 http://www.beliefmedia.com/embed-codepen-wordpress
5
*/
6
 
7
function beliefmedia_embed_codepen_pen($atts) {
8
  $atts = shortcode_atts(array(
9
    'id' => 'http://codepen.io/dissimulate/pen/KrAwx', /* Full Pen URL */
10
    'name' => 'Tearable Cloth',
11
    'height' => '370',
12
    'theme' => '0', /* 0 (default), light, dark */
13
    'preview' => true
14
  ), $atts);
15
 
16
  /* Get ID and username */
17
  $id = rtrim($atts['id'], '/');
18
  $data = explode('/', $id);
19
  $penid = end($data);
20
  $user = array_slice($data, -3, 1);
21
  $user = $user['0'];
22
 
23
  /* Build pen */
24
  $return = '<p><p data-height=&quot;' . $atts['height'] . '&quot; data-theme-id=&quot;' . $atts['theme'] . '&quot; data-slug-hash=&quot;' . $penid . '&quot; data-default-tab=&quot;result&quot; data-user=&quot;' . $user . '&quot;';
25
  if ($atts['preview'] !== false) $return .= ' data-preview=&quot;true&quot;';
26
  $return .= ' class=\'codepen\'>See the Pen <a href=\'' . $id . '\'>' . $atts['name'] . '</a> by ' . $user . ' (<a href=\'http://codepen.io/' . $user . '\'>@' . $user . '</a>) on <a href=\'http://codepen.io\'>CodePen</a>.</p>';
27
  $return .= '<script async src=&quot;//assets.codepen.io/assets/embed/ei.js&quot;></script></p>';
28
 
29
 return $return;
30
}
31
add_shortcode('codepen', 'beliefmedia_embed_codepen_pen');

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.

PHP Function

Used outside of WordPress, the following PHP function may be used.

1
<?php 
2
/*
3
 Embed a CodePen Pen in WordPress with Shortcode
4
 http://www.beliefmedia.com/embed-codepen-wordpress
5
*/
6
 
7
function beliefmedia_embed_codepen_pen($id, $name = '', $height = '370', $theme='0', $preview = true) {
8
 
9
  /* Get ID and username */
10
  $id = rtrim($id, '/');
11
  $data = explode('/', $id);
12
  $penid = end($data);
13
  $user = array_slice($data, -3, 1);
14
  $user = $user['0'];
15
 
16
  /* Build pen */
17
  $return = '<p><p data-height=&quot;' . $height . '&quot; data-theme-id=&quot;' . $theme . '&quot; data-slug-hash=&quot;' . $penid . '&quot; data-default-tab=&quot;result&quot; data-user=&quot;' . $user . '&quot;';
18
  if ($preview !== false) $return .= ' data-preview=&quot;true&quot;';
19
  $return .= ' class=\'codepen\'>See the Pen <a href=\'' . $id . '\'>' . $name . '</a> by ' . $user . ' (<a href=\'http://codepen.io/' . $user . '\'>@' . $user . '</a>) on <a href=\'http://codepen.io\'>CodePen</a>.</p>';
20
  $return .= '<script async src=&quot;//assets.codepen.io/assets/embed/ei.js&quot;></script></p>';
21
 
22
 return $return;
23
}
24
 
25
/* Usage */
26
$id = 'http://codepen.io/dissimulate/pen/KrAwx'; $name = 'Tearable Cloth';
27
echo beliefmedia_embed_codepen_pen($id, $name);
28
?>

Download


Title: Embed a CodePen Pen (WP Plugin)
Description: Embed a CodePen Pen in WordPress with Shortcode (or with a PHP Function).
  Download • Version 0.1, 1.6K, zip, Category: WordPress Plugins (General)
WordPress Shortcodes, (793.0B)    PHP Code & Snippets, (724.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