From df0f53d9ce40b69632a3dc2d1a8ff2da8ac87836 Mon Sep 17 00:00:00 2001 From: Ali Date: Sun, 3 Mar 2024 17:04:42 +0100 Subject: [PATCH 1/2] Fetching random weekly trending poster --- README.md | 2 ++ tmdb-movie-fetcher.php | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/README.md b/README.md index f374aed..969e9f5 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,8 @@ The TMDb Movie Fetcher plugin provides the following shortcodes to display movie - `[tmdb_movie_where_to_watch]`: Displays the streaming providers where the movie is available. - `[tmdb_movie_poster]`: Displays the movie poster image. - `[tmdb_movie_genres]`: Displays the movie genres. +- `[tmdb_random_trending_movie_poster]`: Display the poster of a random weekly trending movie. + ## Usage To use the shortcodes, insert them into the desired location (e.g., post content, page content, widget, etc.) within your WordPress website. diff --git a/tmdb-movie-fetcher.php b/tmdb-movie-fetcher.php index 3337b42..9d0380e 100644 --- a/tmdb-movie-fetcher.php +++ b/tmdb-movie-fetcher.php @@ -199,3 +199,45 @@ function fetch_watch_providers_data($movie_id) { } +// Random trending movie poster shortcode +function tmdb_random_trending_movie_poster_shortcode() { + $random_movie_data = fetch_random_trending_movie(); + + if ($random_movie_data) { + $poster_path = $random_movie_data['poster_path']; + $poster_url = "https://image.tmdb.org/t/p/w500{$poster_path}"; + return '' . $random_movie_data['title'] . ''; + } else { + return 'Error: Could not fetch random trending movie data.'; + } +} + +// Fetch random trending movie data +function fetch_random_trending_movie() { + $time_window = 'week'; // You can change this to 'day' if you want daily trending movies + $api_url = "https://api.themoviedb.org/3/trending/movie/{$time_window}?api_key=" . TMDB_API_KEY; + + $response = wp_remote_get($api_url); + + if (is_wp_error($response)) { + error_log('TMDb Movie Fetcher: ' . $response->get_error_message()); + return false; + } + + $body = wp_remote_retrieve_body($response); + $data = json_decode($body, true); + + if (isset($data['status_code']) && isset($data['status_message'])) { + error_log('TMDb Movie Fetcher: ' . $data['status_message']); + return false; + } + + // Extract a random movie from the list + $random_index = array_rand($data['results']); + $random_movie = $data['results'][$random_index]; + + return $random_movie; +} + +// Register random trending movie poster shortcode +add_shortcode('tmdb_random_trending_movie_poster', 'tmdb_random_trending_movie_poster_shortcode'); From 1bab6552a4618cc20bb783e0355430bbcfb94746 Mon Sep 17 00:00:00 2001 From: Ali Date: Sun, 3 Mar 2024 17:06:13 +0100 Subject: [PATCH 2/2] Fetching random weekly trending poster --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index 969e9f5..5eb538b 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,6 @@ The TMDb Movie Fetcher plugin provides the following shortcodes to display movie - `[tmdb_movie_genres]`: Displays the movie genres. - `[tmdb_random_trending_movie_poster]`: Display the poster of a random weekly trending movie. - ## Usage To use the shortcodes, insert them into the desired location (e.g., post content, page content, widget, etc.) within your WordPress website.