Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fetching random weekly trending movie poster. #1

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ 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.
Expand Down
42 changes: 42 additions & 0 deletions tmdb-movie-fetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 '<img src="' . $poster_url . '" alt="' . $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');