Skip to content

WordPress Structured Data / Schema for Movie (No Plugin)

  • Posted on
  • by
  • 3 min read

About a year ago, I developed this film listing site using WordPress. One of the features I had to implement was that of automatically generating the schema markup for all individual movie listings. This way search engines could pick up and use some important details about a movie such as the director and the film poster.

Initially, I tried a couple of plugins but quickly found out that almost all of them did not support the movie schema type. Eventually I just decided to implement a self-contained solution.

I was fortunate enough to find some resources online which guided me on how to generate the JSON-LD markup. This documentation page proved especially useful.

Movie Schema JSON-LD

The movie schema supports many properties, however Google’s developer documentation indicates the following two properties as a must:

  • name – the title of the movie.
  • image – a high resolution image of that represents the movie (i.e poster) in an aspect ratio of 6:9.

The documentation also recommends including the following additional properties:

  • aggregateRating – the average review score assigned to the movie.
  • director – the director of the movie.
  • review – a nested review of the movie.

The script I used to generate my JSON-LD data was as follows:

<?php

global $schema;
    $schema = array(
        '@context'    => "http://schema.org",
        '@type'       => "Movie",
        'name'        => get_the_title(),
        'image'       => get_the_post_thumbnail_url(),
        'dateCreated' => get_field('release_date') 
    );
// DIRECTOR
       foreach( $directors as $director ){
       $schema['director'] = $director->name;
      }
        function generate_movie_schema ($schema) {
          global $schema;
          echo '<script type="application/ld+json">'. json_encode($schema) .'</script>';
        }
        add_action( 'wp_footer', 'generate_movie_schema', 100 );
        
?>

The above script generates a markup for the image, release date and director properties.

How to use this script?

The above code is more of a guide to give you an idea on how to implement your own solution since your setup and requirements may be different from what I had. Nevertheless, here’s how it works.

In my setup, the above code is implemented at the end of the single template for the movie post type. I used the Advanced Custom Fields (ACF) plugin to create all the necessary fields for that post type i.e. name, the director, release date, genre etc. These fields are declared and fetched in the template to list the movie details in the frontend.

The script above then simply maps these fields to the corresponding properties in the movie schema. When a post is loaded, the script generates the JSON-LD markup inside the body of the HTML page.

For instance, the property name is mapped to the title custom field. In this, case the title of the post is the same as the movie title hence the function get_the_title() which gets the title of the post. Same case for the post’s thumbnail which is equal to the movie’s poster (image property).

The dateCreated property is mapped to a custom field called release_date that contains a date string in the format yyyy-mm-dd which in WordPress formatting equals Y-m-d. This is the date format used by the Schema markup.

On the other hand, the director field is mapped to a repeater field which can contain multiple fields. This is because some movies have more than one director.

Hopefully you get the idea. Once you have implemented the schema, make sure to validate it on the Schema.org validator or Google’s Rich Results Test.

Share:

Leave a Reply

Feel free to share your comments or questions with me. I may not be able to respond immediately so please check later once I've approved your comment.

Your email address will not be published. Required fields are marked *

Kelvin Kathia

Kelvin Kathia is a writer that's passionate about sharing solutions to everyday tech problems. He's the founder and editor of Journey Bytes, a tech blog and web design agency. Feel free to leave him comments or questions regarding this post, or by leaving him a message on the contact page. If you found his content helpful, a donation is much appreciated.