RBA Cash Rate: 4.35% · 1AUD = 0.67 USD · Inflation: 4.1%  
Leading Digital Marketing Experts</strong | 1300 235 433 | Aggregation Enquires Welcome | Book Appointment
Example Interest Rates: Home Loan Variable: 5.69% (5.89%*) • Home Loan Fixed: 5.39% (5.84%*) • Fixed: 5.39% (5.84%*) • Variable: 5.69% (5.89%*) • Investment IO: 5.69% (6.19%*) • Investment PI: 5.55% (6.02%*)

Embed SlideShare Presentations into WordPress with Shortcode

Embed SlideShare Presentations into WordPress with Shortcode

Owned by LinkedIn , SlideShare is web based slide hosting service that allows users to upload files privately or publicly in PowerPoint, PDF, Keynote or OpenDocument formats. Those presentations can then be viewed on the website, mobile applications, or embedded into other websites. The website receives an estimated 58 million unique visitors a month, and has about 16 million registered users, making it one of the most used platforms for education and training.

The primary means of obtaining SlideShare data is via their feature-rich API . The code on this page, however, will fetch oEmbed data and extract the necessary information to render a presentation. Given the large number of our clients that are starting to use LinkedIn (and SlideShare), an API article will likely follow.

The Result

Using the shortcode of [slideshare v="http://www.slideshare.net/haraldf/business-quotes-for-2011"] the following slideshow will show in your WordPress website (where v is the full slideshow URL).

If you wish to exclude the description that sits underneath the slides, use [slideshare v="http://www.slideshare.net/pohjus/introduction-to-php-presentation" description="0"] (meaning description is false). The result is as follows:

To change the way the default footer displays, you should alter the HTML in the shortcode function and style your own attribution footer.

Shortcode Attributes

v

The v refers to the full SlideShare presentation URL. We've used 'v' only because it's consistent with our video shortcodes.

width

The width, or w, should be hard coded into the shortcode function so that you don't have to specifically define it every time you embed a presentation.

height

The h (height) is automatically scaled based on your width. If our scaling doesn't quite work, you should tweek it by defining the height yourself. The height can almost always be left blank.

description

If you choose to provide attribution in a manner other than the default style provided by SlideShare, either alter the shortcode function or use d="0" (meaning description = false). If description="0" it'll be just the iframe presentation that'll be displayed on your page.

cache

The cache defines the amount of time data is stored locally in your WordPress database before a fresh request is made for SlideShare data. Because data is rarely altered by SlideShare authors, updating it once every couple of months is normally sufficient. Alter it as required.

The Code

Copy and paste the WordPress function into your theme's functions.php file or, if you sensibly have one installed, your custom functions plugin.

1
<?php 
2
/*
3
 Embed SlideShare Presentations into WordPress with Shortcode
4
 http://www.beliefmedia.com/slideshare-wordpress
5
*/
6
 
7
function beliefmedia_slideshare_oembed($atts) {
8
 
9
  $atts = shortcode_atts(array(
10
    'v' => '',
11
    'width' => '500',
12
    'height' => '',
13
    'description' => 1,
14
    'cache_temp' => 600,
15
    'cache' => 3600 * 24 * 14
16
  ), $atts);
17
 
18
  /* If no height defined... */
19
  if ($atts['height'] == '') $atts['height'] = floor($atts['width'] / 1.213);
20
 
21
  /* Transient */
22
  $transient = 'bmss_' . md5(serialize($atts));
23
  $cachedslide = get_transient($transient);
24
 
25
   if ($cachedslide !== false) {
26
   return $cachedslide;
27
 
28
   } else {
29
 
30
    /* Get oembed data */
31
    $json = @file_get_contents('http://www.slideshare.net/api/oembed/2?url=' . $atts['v'] . '&format=json');
32
    if ($json !== false) {
33
 
34
        $json_data = json_decode($json, true);
35
 
36
        /* We get all returned data... however, don't use it all */
37
        $temp['thumbnail'] = $json_data['thumbnail'];
38
        $temp['type'] = $json_data['type'];
39
        $temp['slide_image_baseurl_suffix'] = $json_data['slide_image_baseurl_suffix'];
40
        $temp['author_name'] = $json_data['author_name'];
41
        $temp['total_slides'] = $json_data['total_slides'];
42
        $temp['height'] = $json_data['height'];
43
        $temp['slideshow_id'] = $json_data['slideshow_id'];
44
        $temp['conversion_version'] = $json_data['conversion_version'];
45
 
46
        $temp['html'] = $json_data['html']; $html = $temp['html'];
47
 
48
        /* Since building new embed code (due scaling), we'll snatch embed URL */
49
        $regex = '$\b(https?)://[-A-Z0-9+&@#/%?=~_|!:,.;]*[-A-Z0-9+&@#/%=~_|]$i';
50
        preg_match_all($regex, $html, $result); $fullurl = $result['0']['0'];
51
        $temp['url'] = $fullurl;
52
 
53
        $presentationurl = $result['0']['1'];
54
        $temp['presentationurl'] = $presentationurl;
55
 
56
        $temp['version'] = $json_data['version'];
57
        $temp['provider_name'] = $json_data['provider_name'];
58
        $temp['thumbnail_height'] = $json_data['thumbnail_height'];
59
        $temp['provider_url'] = $json_data['provider_url'];
60
        $temp['version_no'] = $json_data['version_no'];
61
        $temp['width'] = $json_data['width'];
62
        $temp['thumbnail_width'] = $json_data['thumbnail_width'];
63
 
64
        /* Author details */
65
        $temp['author_url'] = $json_data['author_url'];
66
          $author = $temp['author_url'];
67
          $author = substr($author, strrpos($v, '/') + 1);
68
          $author = trim($author);
69
          $temp['author'] = $author;
70
 
71
          $temp['slide_image_baseurl'] = $json_data['slide_image_baseurl'];
72
          $temp['title'] = $json_data['title'];
73
 
74
        /* Build the embed code */
75
        $return .= '<div style="text-align: center"><iframe src="' . $temp['url'] .'" width="' . $atts['width'] . '" height="' . $atts['height'] . '" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" style="border:1px solid #cccccc; margin-bottom: 10px; max-width: 100%;" allowfullscreen></iframe></div>';
76
        if ($atts['description']) $return .= '<div style="margin: auto; width: ' . $atts['width'] . 'px;">
77
<div style="text-align: left; font-size: 0.9em;">&plus; <a href="' . $temp['presentationurl'] . '" title="' . $temp['title'] . '" target="_blank" rel="noopener noreferrer"><strong>' . $temp['title'] . '</strong></a> from <a href="' . $temp['author_url'] . '" target="_blank" rel="noopener noreferrer">' . $temp['author_name'] . '</a></div>
78
</div>';
79
        $return .= '';
80
 
81
        /* Save embed data with transient API */
82
        set_transient($transient, $return, $atts['cache']);
83
 
84
        return $return;
85
 
86
        } else {
87
 
88
        $return = 'Slideshare presentation is temporarily unavailable';
89
        set_transient($transient, $return, $atts['cache_temp']);
90
        return $return;
91
 
92
    }
93
 
94
 }
95
}
96
add_shortcode('slideshare','beliefmedia_slideshare_oembed');

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

A PHP function is included as a download below. Usage requires Simple Cache.

Considerations

  • We initially wrote code that stored the data as an option (rather than using the transient API). Because SlideShare gives authors the option to update and edit their presentations, we decided to cache the result for a period of time using the transient API so that we can periodically update data. If the presentation itself is updated before the cache expires, the new presentation will continue to be shown (you can force a new request by altering the cache time).
  • While there's a default "out-of-the-box" embed code returned in SlideShare's JSON response, we've chosen to rebuild it so we can have full control over how the slideshow is styled.
  • We retrieve all the data returned by SlideShare but only cache the resulting HTML. You have access to all returned data so you can easily build your own title area.
  • SlideShare have an API at slideshare.net/developers that provides more advanced features. We'll look at the API in the future.

Download


Title: Embed SlideShare Presentations (WP Shortcode)
Description: Embed SlideShare Presentations into WordPress with Shortcode or PHP.
  Download • Version 0.2, 1.5K, zip, Category: WordPress Shortcodes
PHP Code & Snippets, (1.6K)    

■ ■ ■

 
Download our complimentary 650-page guide on marketing for mortgage brokers. We'll show you exactly how we generate billions in volume for our clients.
Finance Guide, Cropped Top and Bottom
  Timezone: 1 · [ CHANGE ]

RELATED READING

Like this article?

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

Leave a comment