WordPress Plugin: Movie Shortcode

WordPress

F13 Movie Embed Shortcode

0 from 0 ratings

Description:

Using Movie Shortcode you can easily add information about a movie, tv show or episode to a blog post.

Simply enter the shortcode: [movie imdb=”an IMDB ID”] or [Movie title=”A Movie Title”] to a blog post to add an array of information about the movie, tv show or episode.

Optional attributes:
* cachetime=”A time in minutes” – default is 1440 (24 hours)
* plot=”short|full” – default is full

If adding the shortcode with a title, rather than an IMDB ID, the following attributes are also optional to find the correct entry:
* type=”movie|series|episode”
* year=”The year of the movie”

Features:
* Styled appearance
* Shows film title, year, release date
* If showing a series, the total number of seasons is displayed
* If showing an episode, the season and episode number are displayed
* Displays the plot or synopsis
* Displays a poster image if one is available
* If a poster image is available it is added to your media library and loaded locally
* Displays movie stats such as the runtime, genre, awards, director, writer, Actors
* Displays the available languages and country of origin
* Provides the IMDB rating, including a star rating image
* Adds a link to the movie on IMDB
* Utilizes transient caching to reduce page load times and api calls

Arbitrary section

Downloads: 2178

Tags:
  • embed
  • episode
  • movie
  • shortcode
  • tv show

GitHub

Shortcode to add movie information to a WordPress blog, utilising the OMDB API
Forks: 0
Stars: 0
Open issues: 1
Latest tag: None
git clone https://github.com/f13dev/wp-movie-shortcode.git

Introduction

Do you blog about movies or tv shows? It can be a tedious job adding movie information such as synopsis, genres, directors, cast and crew, or finding the correct artwork. Why not use a simple WordPress shortcode to do all the hard work for you.

F13s Movie Shortcode allows you to insert movie, tv show or episode information using an IMDB ID or a title and year, cutting out all the hard work.

Artwork is copied directly to your WordPress Media Library to ensure it’s always available when it’s needed and the whole plugin is cached using transient to ensure a faster page loading time.

Features

  • Styled appearance
  • Shows film title, year, release date
  • If showing a series, the total number of seasons is displayed
  • If showing an episode, the season and episode number are displayed
  • Displays the plot or synopsis
  • Displays a poster image if one is available
  • If a poster image is available it is added to your media library and loaded locally
  • Displays movie stats such as the runtime, genre, awards, director, writer, Actors
  • Displays the available languages and country of origin
  • Provides the IMDB rating, including a star rating image
  • Adds a link to the movie on IMDB
  • Utilizes transient caching to reduce page load times and api calls

Example

The result of [movie title=”The Simpsons Movie” year=”2007″]:

The Simpsons Movie
Year: 2007
Release date: 27 Jul 2007
Plot: Homer adopts a pig who's run away from Krusty Burger after Krusty tried to have him slaughtered, naming the pig "Spider Pig." At the same time, the lake is protected after the audience sink the barge Green Day are on with garbage after they mention the environment. Meanwhile, Spider Pig's waste has filled up a silo in just 2 days, apparently with Homer's help. Homer can't get to the dump quickly so dumps the silo in the lake, polluting it. Russ Cargill, the villainous boss of the EPA, gives Arnold Schwarzenegger 5 options, forcing him to choose 4 (which is, unfortunately, to destroy Springfield) and putting a dome over Springfield to prevent evacuation. Homer, however, has escaped, along with his family. Can he stop the evil Cargill from annihilating his home town, and his family, who have been forced to return to Springfield?
Runtime: 87 min
Genre: Animation, Adventure, Comedy
Awards: Nominated for 1 Golden Globe. Another 5 wins & 33 nominations.
Director: David Silverman
Writer: James L. Brooks (screenplay), Matt Groening (screenplay), Al Jean (screenplay), Ian Maxtone-Graham (screenplay), George Meyer (screenplay), David Mirkin (screenplay), Mike Reiss (screenplay), Mike Scully (screenplay), Matt Selman (screenplay), John Swartzwelder (screenplay), Jon Vitti (screenplay), Joel H. Cohen (consultant writer), John Frink (consultant writer), Tim Long (consultant writer), Michael Price (consultant writer)
Actors: Dan Castellaneta, Julie Kavner, Nancy Cartwright, Yeardley Smith

Language: English
Country: USA
IMDB Rating: 7.3/10 from 294,822 votes
Data provided by OMDb API

The result of [movie imdb=”tt0133093″]:

The Matrix
Year: 1999
Release date: 31 Mar 1999
Plot: A computer hacker learns from mysterious rebels about the true nature of his reality and his role in the war against its controllers.
Runtime: 136 min
Genre: Action, Sci-Fi
Awards: Won 4 Oscars. Another 34 wins & 48 nominations.
Director: Lana Wachowski, Lilly Wachowski
Writer: Lilly Wachowski, Lana Wachowski
Actors: Keanu Reeves, Laurence Fishburne, Carrie-Anne Moss, Hugo Weaving

Language: English
Country: USA
IMDB Rating: 8.7/10 from 1,539,986 votes
Data provided by OMDb API

Installation

  1. Upload the plugin files to the `/wp-content/plugins/plugin-name` directory, or install the plugin through the WordPress plugins screen directly.
  2. Activate the plugin through the ‘Plugins’ screen in WordPress
  3. Add the shortcode [movie imdb=”an IMDB ID”], or [movie title=”A movie title”] to the desired location on your blog

Usage

Simply enter the shortcode: [movie imdb=”an IMDB ID”] or [Movie title=”A Movie Title”] to a blog post to add an array of information about the movie, tv show or episode.

Optional attributes:

  • cachetime=”A time in minutes” – default is 1440 (24 hours)
  • plot=”short|full” – default is full

If adding the shortcode with a title, rather than an IMDB ID, the following attributes are also optional to find the correct entry:

  • type=”movie|series|episode”
  • year=”The year of the movie”

Code

Plugin code:

<?php
/*
Plugin Name: F13 Movie Embed Shortcode
Plugin URI: http://f13dev.com/wordpress-plugin-movie-embed-shortcode/
Description: Embed information about a movie or TV show into a WordPress blog post or page using shortcode.
Version: 1.0
Author: Jim Valentine - f13dev
Author URI: http://f13dev.com
Text Domain: f13-movie-embed-shortcode
License: GPLv3
*/

/*
Copyright 2016 James Valentine - f13dev (jv@f13dev.com)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

// Register the shortcode
add_shortcode( 'movie', 'f13_movie_shortcode');
// Register the css
add_action( 'wp_enqueue_scripts', 'f13_movie_shortcode_style');

/**
 * A function to register the stylesheet
 * @return [type] [description]
 */
function f13_movie_shortcode_style()
{
    wp_register_style( 'f13movie-style', plugins_url('movie-shortcode.css', __FILE__) );
    wp_enqueue_style( 'f13movie-style' );
}

/**
* Function to handle the shortcode
* @param  Array  $atts    The attributes set in the shortcode
* @param  [type] $content [description]
* @return String          The response of the shortcode
*/
function f13_movie_shortcode( $atts, $content = null )
{
    // Get the attributes
    extract( shortcode_atts ( array (
    'imdb' => '', // The IMDB movie ID
    'title' => '', // The title of the movie
    'type' => '', // The type (movie, series, episode)
    'year' => '', // The year of the movie
    'plot' => 'full', // Return full or short plot
    'cachetime' => '1440', // Cache timeout, default 24 hours
    //'rating' => 'true', // Return rotton tomatoes rating (true, false)
  ), $atts ));

  // Set the cache name for this instance of the shortcode
  $cache = get_transient('f13movie' . md5(serialize($atts)));

  if ($cache)
  {
      // If the cache already exists, return it rather than re-creating it
      return $cache;
  }
  else
  {
    // Check if a title or IMDB ID has been entered
    if ($title == '' && $imdb == '')
    {
      // Notify the user that a Title or IMDB ID is required
      $string = 'In order to use this shortcode either the \'imdb\' or \'title\' attributes must be set.<br/>
      Shortcode example:<br />
      [movie imdb=\'imdb_movie_id\'] or [movie title=\'A movie title\']';
    }
    else
    {
      // Store the search query in a variable
      $query = 'http://www.omdbapi.com/?';

      // Check if an IMDB ID has been entered
      if ($imdb != '')
      {
        // If an IMDB ID is present, add it to the query
        // and send the string, no further attributes are
        // required.
        $query .= 'i=' . $imdb;
      }
      else
      {
        // Add the title to the query string
        if ($title != '')
        {
          $query .= 't=' . str_replace(' ', '%20', $title) . '&';
        }
        // Add the type if it is set to an appropriate value
        if ($type == 'move' || $type == 'series' || $type == 'episode')
        {
          $query .= 'type=' . $type . '&';
        }
        // Add the year if it is set and is a number
        if ($year != '' && is_numeric($year))
        {
          $query .= 'y=' . $year . '&';
        }
        // If the plot attribute is set to short, set it short,
        // otherwise set it to full
        if ($plot == 'short')
        {
          $query .= 'plot=short&';
        }
        else
        {
          $query .= 'plot=full';
        }
      }
      // Get the movie data and store it in a variable
      $movie_data = f13_get_movie_data($query);
      // Send the movie data to be formatted
      $string = f13_format_movie_data($movie_data);
    }
    // Multiply the cache timeout by 60 to convert the time in minutes
    // to the time in seconds
    $cachetime = $cachetime * 60;
    // If the cachetime is 0, set it to 1 for an almost instant timeout
    if ($cachetime == 0)
    {
      $cachetime = 1;
    }
    // Set the cache
    set_transient('f13movie' . md5(serialize($atts)), $string, $cachetime);
    // Return the generated string
    return $string;
  }
}

/**
 * A function to format the movie data into a widget
 * @param  array  $data An array of movie data
 * @return String       A formatted rich text string of movie data
 */
function f13_format_movie_data($data)
{
  // Create a variable to store the formatted rich text data
  $rich_text = '';
  // Open the movie container
  $rich_text .= '<div class="f13-movie-container">';
  // Check if a response was generated
  if ($data['Response'] != 'True')
  {
    // If a response was not generated warn the user
    $rich_text .= '<span class="f13-movie-error">The movie, show or episode you requested could not be found.</span>';
  }
  else
  {
    // Open a head div
    $rich_text .= '<div class=f13-movie-head>';

      // Add the title
      $rich_text .= '<div class="f13-movie-title">' . $data['Title'] . '</div>';
      // If the year is available add it
      if ($data['Year'] != '')
      {
        $year = $data['Year'];
        // Check if the year is a range ending in '-'
        if (substr($year, -1) == '-')
        {
          $year = $year . 'present';
        }
        // Add the year
        $rich_text .= '<div class="f13-movie-year"><span>Year: </span>' . $year . '</div>';
      }
      // If the release date is available add it
      if ($data['Released'] != '')
      {
        $rich_text .= '<div class="f13-movie-released"><span>Release date: </span>' . $data['Released'] . '</div>';
      }
      // If the response is a series, input series specific data
      if ($data['Type'] == 'series')
      {
        // Check if totalSeasons is set, if so add it
        if ($data['totalSeasons'] != '')
        {
          $rich_text .= '<div class="f13-movie-totalSeasons"><span>Totla seasons: </span>' . $data['totalSeasons'] . '</div>';
        }
      }
      // If the response is an episode, input episode specific data
      elseif ($data['Type'] == 'episode')
      {
        // Open an episode div
        $rich_text .= '<div class="f13-movie-episode">';
          // If a season is set, add it
          if ($data['Season'] != '')
          {
            $rich_text .= '<span>Season </span>' . $data['Season'] . ' ';
          }
          // If an episode number is set add it
          if ($data['Episode'] != '')
          {
            $rich_text .= '<span>Episode </span>' . $data['Episode'] . ' ';
          }
        // Close the episode div
        $rich_text .= '</div>';
      }

    // Close the head div
    $rich_text .= '</div>';



    // If a plot is set, add it
    if ($data['Plot'] != '')
    {
      $rich_text .= '<div class="f13-movie-plot">
      <span>Plot: </span>' . $data['Plot'] . '</div>';
    }

    // Create  a table to hold the image and stats
    $rich_text .= '<table width="100%" class="f13-movie-table">';
    $rich_text .= '<tr>';

      // If the poster exists add it
      if ($data['Poster'] != '')
      {
        // Get the filenmae from the image URL
        $image_name = end(explode('/', $data['Poster']));
        // Find if the file already exists in attachments
        $image_id = f13_get_attachment_id($image_name);
        if ($image_id == null)
        {
          // If the image file does not already exist try and
          // add it to the media library.

          // Require files used to sideload
          require_once(ABSPATH . 'wp-admin/includes/media.php');
          require_once(ABSPATH . 'wp-admin/includes/file.php');
          require_once(ABSPATH . 'wp-admin/includes/image.php');

          // Attempt to sideload image
          media_sideload_image($data['Poster'], get_the_ID(), $data['Title']);
          // Get the newly sideloaded image
          $image_id = f13_get_attachment_url($image_name);
          // Get the image url
          $image_url = wp_get_attachment_url($image_id);
        }
        else
        {
          // If the image already exists, use the
          // image id already obtained.
          $image_url = wp_get_attachment_url($image_id);

        }
        // Check if the image id is a number, if so add
        // the image.
        if (is_numeric($image_id) && $image_id != null)
        {
          // Create a table cell to put the image in
          $rich_text .= '<td style="width: 30%; vertical-align:top; padding-right: 10px;">';
          // Add the image using the pre-found image url
          $rich_text .= '<img src="' . $image_url . '" />';
          // Close the table cell
          $rich_text .= '</td>';
          // Close the image div
        }
      }

      // Add a table cell to hold the movie stats
      $rich_text .= '<td style="vertical-align: top">';

        // If a runtime is set, add it
        if ($data['Runtime'] != '')
        {
          $rich_text .= '<div class="f13-movie-runtime"><span>Runtime: </span>' . $data['Runtime'] . '</div>';
        }
        // If a genre is set, add it
        if ($data['Genre'] != '')
        {
          $rich_text .= '<div class="f13-movie-genre"><span>Genre: </span>' . $data['Genre'] . '</div>';
        }
        // If awards is set, add it
        if ($data['Awards'] != 'N/A' && $data['Awards'] != '')
        {
          $rich_text .= '<div class="f13-movie-awards"><span>Awards: </span>' . $data['Awards'] . '</div>';
        }

      // If a director is set, add it
      if ($data['Director'] != 'N/A')
      {
        $rich_text .= '<div class="f13-movie-director"><span>Director: </span>' . $data['Director'] . '</div>';
      }
      // If a writer is set, add it
      if ($data['Writer'] != 'N/A')
      {
        $rich_text .= '<div class="f13-movie-writer"><span>Writer: </span>' . $data['Writer'] . '</div>';
      }
      // If actors is set, add it
      if ($data['Actors'] != 'N/A')
      {
        $rich_text .= '<div class="f13-movie-actors"><span>Actors: </span>' . $data['Actors'] . '</div>';
      }
    // Close the table cell
    $rich_text .= '</td>';
    // Close the table row
    $rich_text .= '</tr>';
    // Close the table
    $rich_text .= '</table>';

    // Create a localization div
    $rich_text .= '<div class="f13-movie-localization">';
      // If a language is set, add it
      if ($data['Language'] != 'N/A')
      {
        $rich_text .= '<div class="f13-movie-language"><span>Language: </span>' . $data['Language'] . '</div>';
      }
      // If a country is set, add it
      if ($data['Country'] != 'N/A')
      {
        $rich_text .= '<div class="f13-movie-country"><span>Country: </span>' . $data['Country'] . '</div>';
      }
    // Close the localization div
    $rich_text .= '</div>';

    // Open a rating div
    $rich_text .= '<div class="f13-movie-rating">';
      // Check if a valid rating is set
      if (is_numeric($data['imdbRating']))
      {
        // If a valid rating is set add the IMDB rating title
        $rich_text .= '<span>IMDB Rating: </span>';
        // If imdbVotes is a valid number, append the number of voters
        if (is_numeric(str_replace(',', '', $data['imdbVotes'])))
        {
          $rich_text .= ' ' . $data['imdbRating'] . '/10 from ' . $data['imdbVotes'] . ' votes';
        }
        // Open the stars div
        $rich_text .= '<div class="f13-movie-stars">';
        // Show the IMDB star rating
        $rich_text .= f13_get_movie_rating_stars($data['imdbRating']);
        // Close the stars div
        $rich_text .= '</div>';
      }
      else
      {
        // If no valid rating is set, generate the 0 stars image
        $rich_text .= f13_get_movie_rating_stars(0.0);
      }
    // Close the rating div
    $rich_text .= '</div>';

    // If the IMDB id is present, add a link to the IMDB page
    if ($data['imdbID'] != '' || $data['imdbID'] != 'N/A')
    {
      $rich_text .= '<div class="f13-movie-imdb"><a href="http://www.imdb.com/title/' . $data['imdbID'] . '">View ' . $data['Title'] . ' on IMDB</a></div>';
    }
    $rich_text .= '<div class="f13-movie-powered-by">Data provided by <a href="http://omdbapi.com">OMDb API</a></div>';
  }

  // Close the movie container
  $rich_text .= '</div>';
  return $rich_text;
}

/**
 * A function to retrieve the movie information from the
 * Open Movie Databse
 * @param  $query  The query string to be appended to the url.
 * @return         A decoded array of information about the movie.
 */
 function f13_get_movie_data($url)
 {
   // Start curl
   $curl = curl_init();
   // Set curl options
   curl_setopt($curl, CURLOPT_URL, $url);
   curl_setopt($curl, CURLOPT_HTTPGET, true);
   // Set curl headers
   curl_setopt($curl, CURLOPT_HTTPHEADER, array(
     'Content-Type: application/json',
     'Accept: application/json'
   ));
   // Set the user agent
   curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
   // Set curl to return the response, rather than print it
   curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
   // Get the results
   $result = curl_exec($curl);
   // Close the curl session
   curl_close($curl);
   // Decode the results
   $result = json_decode($result, true);
   // Return the results
   return $result;
 }

/**
 * A function to generate a star rating image
 * @param  float  $aRating A float representing a rating out of 10
 * @return String          A string of rich data showing a star rating
 */
function f13_get_movie_rating_stars($aRating)
{
  $string = '';
  for ($x = 1; $x < $aRating; $x++ )
  {
    $string .= '<img src="' . plugin_dir_url( __FILE__ ) . 'img/star-full.png" />';
  }
  if (strpos($aRating, '.'))
  {
    $string .= '<img src="' . plugin_dir_url(__FILE__) . 'img/star-half.png" />';
    $x++;
  }
  while ($x <= 10)
  {
    $string .= '<img src="' . plugin_dir_url(__FILE__) . 'img/star-empty.png" />';
    $x++;
  }
  return $string;
}


// retrieves the attachment ID from the filename
function f13_get_attachment_id($file_name) {
	global $wpdb;
  // Search the database for an attachment ending with the filename
	$attachment = $wpdb->get_col($wpdb->prepare("SELECT post_id FROM {$wpdb->base_prefix}postmeta WHERE meta_key='_wp_attached_file' AND meta_value LIKE %s;", '%' . $file_name ));
  // Returns the post ID or null
  if ($attachment[0] == null || $attachment[0] == '')
  {
    // If the post ID is not valid return null
    return null;
  }
  else
  {
    // Otherwise return the valid post ID
    return $attachment[0];
  }
}

CSS:

.f13-movie-head
{
  border-bottom: 1px solid #ccc;
  padding-bottom: 10px;
  margin-bottom: 10px;
}

.f13-movie-container
{
  background: #efefef;
  border-radius: 10px;
  padding: 10px;
  margin-bottom: 1em;
}

.f13-movie-error
{
  color: #ff0000;
}

.f13-movie-title
{
  font-size: 1.5em;
  font-weight: bold;
}

.f13-movie-year
{
  display: inline-block;
  padding-right: 10px;
}

.f13-movie-year > span
{
  font-weight: bold;
}

.f13-movie-content
{

}

.f13-movie-released
{
  display: inline-block;
  padding-right: 10px;
}

.f13-movie-released > span
{
  font-weight: bold;
}

.f13-movie-episode
{
  display: inline-block;
  padding-right: 10px;
}

.f13-movie-episode > span
{
  font-weight: bold;
}

.f13-movie-totalSeasons
{

}

.f13-movie-plot
{

}

.f13-movie-stats
{
  width: 55%;
  display: inline-block;

}

.f13-movie-image-container
{
  min-width: 200px;
  height: auto;
  display: inline-block;
  width: 40%;
  margin-right: 5%;
  position: relative;
  top: 0px;
}

.f13-movie-image-mid > img
{
  width: 40%;
  height: auto;
  float: left;
  margin:10px;
}

.f13-movie-plot
{
  margin-bottom: 10px;
  padding-bottom: 10px;
  border-bottom: 1px solid #ccc;
}

.f13-movie-plot > span
{
  display: block;
  font-weight: bold;
}

.f13-movie-language
{
  display: inline-block;
  margin-right: 10px;
}

.f13-movie-language > span
{
  font-weight: bold;
}

.f13-movie-country
{
  display: inline-block;
  margin-right: 10px;
}

.f13-movie-rating > span
{
  font-weight: bold;
}

.f13-movie-country > span
{
  font-weight: bold;
}

.f13-movie-runtime > span
{
  font-weight: bold;
}


.f13-movie-genre > span
{
  font-weight: bold;
}

.f13-movie-awards > span
{
  font-weight: bold;
}

.f13-movie-director
{
  display: inline-block;
  padding-right: 10px;
}

.f13-movie-director > span
{
  font-weight: bold;
}

.f13-movie-writer
{
  display: inline-block;
  padding-right: 10px;
}

.f13-movie-writer > span
{
  font-weight: bold;
}

.f13-movie-actors
{
  display: inline-block;
  padding-right: 10px;
}

.f13-movie-actors > span
{
  font-weight: bold;
}

Screenshot

An example of F13s Movie Shortcode in use.

An example of F13s Movie Shortcode in use.

2 thoughts on “WordPress Plugin: Movie Shortcode

  1. I have been using F13 Movie Embed Shortcode plugin and it was awsome.
    But recently It got some errors and failed to fetch data from IMDB.
    Instead it shows error “The movie, show or episode you requested could not be found. ” formy new and older posts too.

    • Hello Shahzab Khan.

      First of all, I apologise for the delay in my reply. After checking the plugin at my end I can see the same issue is happening here; this is most likely a case where IMDB have altered their API.

      I will look into resolving this issue and update the plugin accordingly. At some point over the next couple of weeks you should receive an update notification for the plugin via your WordPress sit admin panel.

      Regards
      Jim

Leave a Reply