Integrating OMDb API with WordPress and ACF/Metabox

tdrayson

I have wanted to connect to OMDb API for a long time. The main reason was to be used on my personal website. This idea was to be able to give a rating on movies that I have seen. This tutorial pulls the data straight into Advanced Custom Fields when you save the post. 

Click here to skip to full ACF code

Update: I have added Metabox code at the bottom. View Metabox Code

First you need to get an API key from the OBDb Website

Once you have your API Key, we need to create the ACF Fields that we want the data to be stored in.

ACF Fields

I created 2 groups of ACF Fields:

I used the ACF Star Rating field created by Kevin Ruscoe.

Creating the function

First we create a function that passes in the current $post_id and we call the save_post action after our function.


        function get_movie_data($post_id){
// Our code goes here
}
add_action('save_post', 'get_movie_data');
      
Copy

Next we need to make a check to see if the current post is equal to the post type we want and the post does not have the status of trash (This will cause issues down the line if not added). In my case my post type is called entertainment.


        if ( get_post_type() == 'entertainment' && get_post_status() != 'trash') {
// All our code goes here
}
      
Copy

Making the request

There are 2 ways we can retrieve the data. Via the IMDb ID or the movie name/title. The ID is more accurate, however you might like to populate your fields by the movie title.

Retrieve data via IMDb ID

To make the request, we make use of the WordPress function wp_remote_get. We then pass the IMDb ID from our custom field, and API key which you created. Make sure you replace API_KEY_HERE with your API Key.


        $api_key = 'API_KEY_HERE';
$imdb_id = get_field('ent_imdb_id', $post_id);

$request = wp_remote_get( 'http://www.omdbapi.com/?apikey='.$api_key.'&i='.$imdb_id );

if( is_wp_error( $request ) ) {
	return false; // Bail early
}

$body = wp_remote_retrieve_body( $request );
$data = json_decode( $body );
      
Copy

Retrieve data via title

We can also retrieve movie data by title. To do this we need to change i to t on line 4 and urlencode the $imdb_title so that we add + where we have spaces.

Note: I have updated the $imdb_id variable name to imdb_title.


        $api_key = 'API_KEY_HERE';
$imdb_title = get_field('ent_imdb_title', $post_id);

$request = wp_remote_get( 'http://www.omdbapi.com/?apikey='.$api_key.'&t='.$imdb_title );

if( is_wp_error( $request ) ) {
	return false; // Bail early
}

$body = wp_remote_retrieve_body( $request );
$data = json_decode( $body );
      
Copy

Storing data in variables

We now need to store the data that we want to variables. 


        $plot = $data->Plot;
$released = $data->Released;
$rated = $data->Rated;
$director = $data->Director;
$genres = $data->Genre;
$poster = $data->Poster;
$imdb_rating = $data->imdbRating;
$rotten_tomatoes = $data->Ratings[1]->Value;
if($rotten_tomatoes){ $rotten_tomatoes = rtrim($rotten_tomatoes, '%');}
      
Copy

You can view all the available data that is returned by the API by going to the example on their website

Updating custom fields

The last thing we need to do is update our custom fields with the variables that we just stored from the json request.


        update_field('ent_mt_plot', $plot, $post_id);
update_field('ent_mt_released', $released, $post_id);
update_field('ent_mt_rated', $rated, $post_id);
update_field('ent_mt_director', $director, $post_id);
update_field('genres', $genres, $post_id);
update_field('ent_mt_poster', $poster, $post_id);
update_field('ent_mt_imdb_rating', $imdb_rating, $post_id);
update_field('ent_mt_rotten_tomatoes', $rotten_tomatoes, $post_id);
      
Copy

Update_field ACF Docs

ACF Code

When you put it all together you get this:
I have commented out the part for populating data by title (Lines 5 & 8)


        function get_movie_data($post_id){
  if ( get_post_type() == 'entertainment' && get_post_status() != 'trash') {
 $api_key = 'API_KEY_HERE';
 $imdb_id = get_field('ent_imdb_id', $post_id);
 //$imdb_title = get_field('ent_imdb_title', $post_id);

$request = wp_remote_get( 'http://www.omdbapi.com/?apikey='.$api_key.'&i='.$imdb_id );
//$request = wp_remote_get( 'http://www.omdbapi.com/?apikey='.$api_key.'&t='.$imdb_title );

if( is_wp_error( $request ) ) {
	return false; // Bail early
}

$body = wp_remote_retrieve_body( $request );
$data = json_decode( $body );

$plot = $data--->Plot;
$released = $data->Released;
$rated = $data->Rated;
$director = $data->Director;
$genres = $data->Genre;
$poster = $data->Poster;
$imdb_rating = $data->imdbRating;
$rotten_tomatoes = $data->Ratings[1]->Value;
if($rotten_tomatoes){ $rotten_tomatoes = rtrim($rotten_tomatoes, '%');}

update_field('ent_mt_plot', $plot, $post_id);
update_field('ent_mt_released', $released, $post_id);
update_field('ent_mt_rated', $rated, $post_id);
update_field('ent_mt_director', $director, $post_id);
update_field('genres', $genres, $post_id);
update_field('ent_mt_poster', $poster, $post_id);
update_field('ent_mt_imdb_rating', $imdb_rating, $post_id);
update_field('ent_mt_rotten_tomatoes', $rotten_tomatoes, $post_id);

    }}
add_action('save_post', 'get_movie_data');
      
Copy

Metabox Code


        function get_movie_data($post_id){
if (get_post_type() == 'entertainment' && get_post_status() != 'trash') {
 $api_key = 'API_KEY_HERE';
 $imdb_id = rwmb_meta( 'imdb_id');

$request = wp_remote_get( 'http://www.omdbapi.com/?&apikey='.$api_key.'&i='.$imdb_id );

if( is_wp_error( $request ) ) {
	return false; // Bail early
}

$body = wp_remote_retrieve_body( $request );
$data = json_decode( $body );

$plot = $data->Plot;
$released = $data->Released;
$director = $data->Director;
//$genres = $data->Genre;
$runtime = $data->Runtime;
$actors = $data->Actors;
$country = $data->Country;
$language = $data->Language;
//$poster = $data->Poster;

rwmb_set_meta( $post_id, 'plot', $plot);
rwmb_set_meta( $post_id, 'premiere_date', $released);  
rwmb_set_meta( $post_id, 'directors', $director);
//rwmb_set_meta( $post_id, 'film_genre', $genres);
rwmb_set_meta( $post_id, 'running_time', $runtime); 
rwmb_set_meta( $post_id, 'actors', $actors); 
rwmb_set_meta( $post_id, 'country', $country); 
rwmb_set_meta( $post_id, 'language', $language);
//rwmb_set_meta( $post_id, 'product_image', $poster);   

}}
add_action('save_post', 'get_movie_data');
      
Copy

👋🏻 Weekly Tutorial Digest

I send out a weekly newsletter with links to new tutorials written in the last week, you can subscribe below.

Newsletter

🔒I won't send you spam, I promise