Am using WordPress.. and I want to make variable $term_id to be dynamic based on selected episode in dropdown..
Currently I have this Ajax Functions and JS but I want all to be replaces with.. the ability to fetch current episode term id and update term_id in html from the selected drop down..
Here is my current functions.php
// We are adding Support for Ajax Playing Episodes
// Enqueue the JavaScript file
function enqueue_custom_scripts() {
wp_enqueue_script('custom-ajax', get_template_directory_uri() . '/resources/assets/js/episodes.js', array('jquery'), null, true);
wp_localize_script('custom-ajax', 'ajaxurl', admin_url('admin-ajax.php'));
}
add_action('wp_enqueue_scripts', 'enqueue_custom_scripts');
// Handle AJAX request to get episode video URL
function get_episode_video() {
$episode_id = intval($_GET['episode_id']);
if ($episode_id) {
$component = new TORONITEScomponentsepisodes($episode_id);
$videos = $component->format_videos();
if ($videos->has) {
$firstVideoLink = reset($videos->results)['links'][0];
$term_id = get_the_terms($episode_id, 'episode_taxonomy')[0]->term_id; // Get the term ID for the episode
$video_url = home_url('/?trembed=' . $firstVideoLink['opt'] . '&trid=' . $term_id . '&trtype=2'); // Use the term ID in the URL
wp_send_json_success(['video_url' => $video_url]);
}
}
wp_send_json_error(['message' => 'Video not found']);
}
add_action('wp_ajax_get_episode_video', 'get_episode_video');
add_action('wp_ajax_nopriv_get_episode_video', 'get_episode_video');
And here is my episodes.js code
document.addEventListener('DOMContentLoaded', function() {
// Function to update the video player
function updatePlayer(src) {
console.log('Video URL:', src); // Log video URL for debugging
const iframe = document.createElement('iframe');
iframe.width = '560';
iframe.height = '315';
iframe.src = src;
iframe.frameBorder = '0';
iframe.allow = 'accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture';
iframe.allowFullscreen = true;
const playerContainer = document.querySelector('.player-container');
playerContainer.innerHTML = '';
playerContainer.appendChild(iframe);
}
// Event listener for episode selection
document.querySelectorAll('.seasons-lst a').forEach(function(episodeLink) {
episodeLink.addEventListener('click', function(event) {
event.preventDefault();
const episodeId = this.getAttribute('data-episode-id');
fetch(`/wp-admin/admin-ajax.php?action=get_episode_video&episode_id=${episodeId}`)
.then(response => response.json())
.then(data => {
updatePlayer(data.video_url);
})
.catch(error => console.error('Error fetching episode video:', error));
});
});
// Event listener for server selection
document.querySelectorAll('.server-link').forEach(function(serverLink) {
serverLink.addEventListener('click', function(event) {
event.preventDefault();
const videoSrc = atob(this.getAttribute('data-src'));
updatePlayer(videoSrc);
});
});
// Initial play icon click to load first video
const playIconWrapper = document.querySelector('.play-icon-wrapper');
if (playIconWrapper) {
playIconWrapper.addEventListener('click', function() {
const videoPlaceholder = document.querySelector('.video-placeholder');
const iframeHtml = videoPlaceholder.getAttribute('data-iframe');
videoPlaceholder.innerHTML = iframeHtml;
});
}
});
Here is my full code..
Term id should be defined before this line and it should be dynamic based on selected episode
$current = new TORONITEScomponentsepisodes($term_id, $post->ID);
Full sngl-srs.php code below.. Thanks in advance.. I want everything to be done without leaving a page
<?php
$details = new TORONITEScomponentsseries($post->ID);
$views = get_theme_mod('users_disabled_views', false);
if (!$views) {
$details->set_views();
}
$current = new TORONITEScomponentsepisodes($term_id, $post->ID);
$season_number = $current->season_number('%01d');
$seasons = $season_number->results == 0 ? 'special' : $season_number->results;
$episode_number = $current->episode_number('%01d');
$episodes = $episode_number->results == 0 ? 0 : $episode_number->results;
?>
<div class="bd cont">
<?php
$series_top = get_theme_mod('advertising_series_top', '');
if (!empty($series_top)) {
echo '<div class="dvr-728">';
echo $series_top;
echo '</div>';
}
$breadcrumb = get_theme_mod('series_disabled_breadcrumb', false);
if (!$breadcrumb) {
get_template_part('resources/views/partials/srs', 'prtl1');
}
$videos = $current->format_videos();
$links = $current->format_links();
if ($videos->has) {
$content = $videos->results;
echo '<div class="player-and-options-container">';
// Player container
echo '<div class="player-container">';
// Placeholder for the first video iframe
$firstVideoLink = reset($content)['links'][0];
$iframeSrc = home_url('/?trembed=' . $firstVideoLink['opt'] . '&trid=' . $term_id . '&trtype=2');
$iframeHtml = '<iframe width="560" height="315" src="' . $iframeSrc . '" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>';
echo '<div class="video-placeholder" style="background-color: #000;" data-iframe="'.htmlspecialchars($iframeHtml, ENT_QUOTES, 'UTF-8').'">';
$cover = get_post_meta($post->ID, 'backdrop_hotlink', true);
echo '<img src="https://image.tmdb.org/t/p/original'.$cover.'" style="width: 100%; height: 100%;" alt="Video Placeholder">';
echo '<div class="play-icon-wrapper"></div>';
echo '</div>';
// Insert your controls HTML here
echo '<div id="movie-managers">';
echo '<div class="movie-managers-wrap">';
echo '<div class="trailer movie-manager" data-toggle="modal" data-target="#modal"><i class="fab fa-youtube"></i> Trailer</div>';
echo '<div class="comment movie-manager" data-go="#comment"><i class="fa fa-comment"></i> Comments</div>';
echo '<div class="bookmark inactive movie-manager" data-bookmark="Favorite" id="4735"><i class="fa fa-heart"></i> Favorite</div>';
echo '<div class="movie-manager report" data-toggle="modal" data-target="#report-video"><i class="fa fa-exclamation-triangle"></i> Report</div>';
echo '</div>';
echo '</div>';
echo '</div>'; // End of player container
// Video Options container
echo '<div class="video-options-container">';
// Note
echo '<div class="note-container">';
echo '<div class="note">If you enjoy the movie, please consider sharing it with your friends. Thank you!</div>';
echo '</div>';
// Video options list
echo '<div class="video-options-list">';
$servers = []; // Initialize array to group links by server
// Group video links by server
foreach ($content as $item) {
foreach ($item['links'] as $link) {
$servers[$link['srv']][] = $link; // Group by server name
}
}
// Display options for each server
foreach ($servers as $serverName => $links) {
echo '<div class="server-group">'; // Server group container
foreach ($links as $link) {
echo '<a href="#!" class="server-link" data-src="' . base64_encode(home_url('/?trembed=' . $link['opt'] . '&trid=' . $term_id . '&trtype=2')) . '" data-lmt="' . $link['lmt'] . '" data-option>';
echo '<div class="server-name"><span> Server</span><span>' . htmlspecialchars($serverName) . '</span></div>';
echo '</a>';
}
echo '</div>'; // End of server group container
}
echo '</div>'; // End of video options list container
// Adding Seasons and Episodes List
$seasons = wp_get_post_terms($post->ID, 'seasons', array(
'orderby' => 'meta_value_num',
'order' => 'ASC',
'fields' => 'all',
'meta_query' => array(
[
'key' => 'season_number',
'type' => 'NUMERIC',
]
)
));
if ($seasons) {
echo '<div class="seasons aa-crd" x-data="{ selectedSeason: 0, tab: 0 }">';
// Dropdown for seasons
echo '<select x-model="selectedSeason" @change="tab = selectedSeason">';
foreach ($seasons as $k => $season) {
$season_number = get_term_meta($season->term_id, 'season_number', true);
$selected = ($k == 0) ? 'selected' : '';
echo '<option value="' . $k . '" ' . $selected . '>Season ' . $season_number . '</option>';
}
echo '</select>';
foreach ($seasons as $k => $season) {
$term = $season->term_id;
$component = new TORONITEScomponentsseasons($term, $post->ID);
echo '<div class="seasons-bx" x-show="tab == ' . $k . '">';
echo '<div :class="{ 'seasons-tt aa-crd-lnk': true, 'on': tab == ' . $k . ' }" x-clock>';
echo '<div>';
$season_number = $component->season_number('%01d');
$number = $season_number->results == 0 ? __('Special', 'toronites') : $season_number->results;
printf('<p>%1$s <span>%2$s</span> <i class="fa-chevron-down"></i></p>', __('Season', 'toronites'), $number);
echo '</div>';
echo '</div>';
$season_number = $component->season_number('%01d');
$number = $season_number->results == 0 ? 'special' : $season_number->results;
if ($number == 'special') {
$args = array(
array(
'relation' => 'AND',
'episode_number' => array(
'key' => 'episode_number',
'type' => 'NUMERIC'
),
'season_number' => array(
'key' => 'season_special',
'compare' => '=',
'value' => 1
)
)
);
} else {
$args = array(
array(
'relation' => 'AND',
'episode_number' => array(
'key' => 'episode_number',
'type' => 'NUMERIC'
),
'season_number' => array(
'key' => 'season_number',
'compare' => '=',
'value' => $number
)
)
);
}
$episodes = wp_get_post_terms($post->ID, 'episodes', array(
'orderby' => 'meta_value_num',
'order' => 'ASC',
'fields' => 'all',
'meta_query' => $args,
'number' => 0
));
if ($episodes) {
echo '<ul class="seasons-lst anm-a">';
foreach ($episodes as $episode) {
$term_id = $episode->term_id;
$slug = esc_url(get_term_link($episode));
$component = new TORONITEScomponentsepisodes($episode->term_id);
$date = $component->date();
if ($date->has) {
$date = $date->results;
} else {
$date = '';
}
$formats = $component->formats();
if ($formats->has) {
$formats = sprintf('%01d', $formats->results);
} else {
$formats = '';
}
$name = $component->name();
if ($name->has) {
$name = $name->results;
} else {
$name = '';
}
$nsx = '';
if ($season_number->has) {
$nsx = $season_number->results;
} else {
$nsx = '';
}
$videos = $component->format_videos();
$cover = get_post_meta($post->ID, 'backdrop_hotlink', true);
$thumbnail = "https://image.tmdb.org/t/p/original" . $cover;
if ($videos->has) {
echo '<li><a href="' . $slug . '"><div><div><h3 class="title"><span>S' . $nsx . '-E' . $formats . '</span> ' . $name . '</h3></div></div></a></li>';
} else {
echo '<li><div><div><h3 class="title"><span>S' . $nsx . '-E' . $formats . '</span> ' . $name . '</h3></div><div></div></div></li>';
}
}
echo '</ul>';
}
echo '</div>';
}
echo '</div>';
}
echo '</div>'; // End of video options container
echo '</div>'; // End of player and options container
}
$series_bottom = get_theme_mod('advertising_series_bottom', '');
if (!empty($series_bottom)) {
echo '<div class="dvr-728">';
echo $series_bottom;
echo '</div>';
}
?>
<section class="section single">
<article class="post single">
<div class="dfxb">
<aside>
<div class="post-thumbnail">
<figure>
<?php
$size = get_theme_mod('series_thumbnail', 'medium');
if ($size !== 'none') {
echo $details->thumbnail($size, get_the_title());
} else {
echo $details->thumbnail('w342', get_the_title());
}
?>
</figure>
</div>
</aside>
<div>
<header class="entry-header">
<h1 class="entry-title"><?php the_title(); ?></h1>
<div class="entry-meta">
<?php
if (!$views) {
$_views = $details->views();
if ($_views->has) {
printf('<span><i class="fa-eye"></i> %1$s %2$s</span>', number_format($_views->results), __('Views', 'toronites'));
}
}
$release = $details->release();
if ($release->has) {
printf('<span class="year">%1$s</span>', $release->results);
}
$minutes = $details->minutes();
if ($minutes->has) {
printf('<span class="duration">%1$s</span>', $minutes->results);
}
$ranking = $details->ranking();
if ($ranking->has) {
printf('<span class="rating fa-star"><span>%1$s</span></span>', $ranking->results);
}
$genres = $details->genres();
if ($genres->has) {
printf('<span class="categories">%1$s</span>', str_replace(',', '', $genres->results));
}
$seasons = $details->seasons();
if ($seasons->has) {
$seasons = $seasons->results;
} else {
$seasons = '';
}
$episodes = $details->episodes('%01d');
if ($episodes->has) {
$episodes = $episodes->results;
} else {
$episodes = '';
}
if ($seasons and $episodes) {
printf('<span class="season-episode">%1$s %2$s - %3$s %4$s</span>', $seasons, __('Seasons', 'toronites'), $episodes, __('Episodes', 'toronites'));
}
?>
</div>
</header>
<div class="entry-content">
<?php the_content(); ?>
</div>
<ul class="details-lst">
<?php
$director = $details->director();
if ($director->has) {
printf('<li class="rw sm"><span>%1$s</span><span>%2$s</span></li>', __('Director', 'toronites'), $director->results);
}
$genres = $details->genres();
if ($genres->has) {
printf('<li class="rw sm"><span>%1$s</span><span>%2$s</span></li>', __('Genres', 'toronites'), $genres->results);
}
$casts = $details->casts(5);
if ($casts->has) {
printf('<li class="rw sm"><span>%1$s</span><span>%2$s</span></li>', __('Cast', 'toronites'), $casts->results);
}
?>
</ul>
</div>
</div>
<?php
$share = get_theme_mod('series_disabled_share', false);
if (!$share) {
get_template_part('resources/views/partials/srs', 'prtl2');
}
?>
</article>
<?php
if (comments_open() || get_comments_number()) {
comments_template();
}
$tags = $details->tags();
if ($tags->has) {
printf('<div class="tags"><span class="fa-hashtag">%1$s</span><div class="tagcloud">%2$s</div></div>', __('Keywords', 'toronites'), $tags->results);
}
$related = get_theme_mod('series_disabled_related', false);
if (!$related) {
get_template_part('resources/views/partials/srs', 'prtl3');
}
?>
</section>
</div>