Vibes

HOWTO: Redirect to a Random YouTube Video on WordPress Website

Learn how to create a random YouTube video redirect easily!

By ♟ Maggew.comUpdated   10 Views
wordpress redirecting random youtube video

This is ideal if you love a channel and just want to enjoy a random video. One of my favorites features a Stellaris-style soundtrack, with regular updates and great quality. I know I’ll enjoy every video, especially since they’re all an hour long.

When I want to get into the moment, I just click a link and continue what I was doing, usually playing it through my Bluetooth speaker or desktop sound system at low volume — https://maggew.com/random-SciFiAmbience

This shouldn’t take you too long, maybe 10 minutes to get up and running for something that can pay serious dividends to future you.

  1. Download addon to Video Extract URLs — https://chromewebstore.google.com/detail/youtube-url-extractor/jmilibpbdpajjnabchfpfmmmjgbimefo
  2. Visit favorite YouTube channel and scroll to very bottom of videos page so you can “fetch” all the URLS instead of just the top 10 or 20 that load
    1. https://www.youtube.com/@Futurescapes-SciFiAmbience/videos
  3. Click Extract URLs than Copy URLs to clipboard
wordpress redirecting random youtube video (2)
extract all video links from a specific youtube channel and click copy
  1. Visit an online tool to add a prefix and suffix… we want to prepend a quote symbol and append a quote symbol with a comma… I like this one — https://text-tools.dev/prefix-suffix-lines
wordpress redirecting random youtube video (1)
paste all video links into a tool to match code syntax
  1. After you copy to clipboard, we’ll want to create a new snippet than paste and replace the lines below in the code example
  2. Log into WordPress website and Add new snippet than copy / paste the text below and activate.
  3. Visit yourdomain.com/future-SciFiAmbience
  4. *(optional) change line #4 random_slug to whatever you want
  5. *(optional) update your blank new tab landing page or create a bookmark for you visit when you wanna vibe out to some jams… 
<?php
// Add this snippet to your theme's functions.php or use a plugin like "Code Snippets" to include it.
add_action('init', function() {
    $random_slug = 'random-SciFiAmbience'; // The slug to trigger the random video
    $request_uri = trim($_SERVER['REQUEST_URI'], '/');

    if ($request_uri === $random_slug) {
        // Define the list of YouTube video URLs
        $youtube_videos = [
            "https://www.youtube.com/watch?v=G_-XgbDVUdQ&t=406s",
            "https://www.youtube.com/watch?v=_OcDW4p7kHo",
            "https://www.youtube.com/watch?v=VTrMU3WS-lY&t=1s",
            "https://www.youtube.com/watch?v=kiIW2tSfSrk",
            "https://www.youtube.com/watch?v=7sju0dQbkJc",
            // Add the rest of your URLs here...
            "https://www.youtube.com/watch?v=HRu2HVLP5iA"
        ];

        // Choose a random video from the list
        $random_video = $youtube_videos[array_rand($youtube_videos)];

        // Redirect to the random video
        wp_redirect($random_video);
        exit;
    }
});

People also search:

  • Easily Add Random YouTube Video Links to Your WordPress Site
  • Simple PHP Code to Show Random YouTube Videos in WordPress
  • Create a Random YouTube Video Redirect in WordPress with This Snippet

This is ideal if you love a channel and just want to enjoy a random video. One of my favorites features a Stellaris-style soundtrack, with regular updates and great quality. I know I’ll enjoy every video, especially since they’re all an hour long.

When I want to get into the moment, I just click a link and continue what I was doing, usually playing it through my Bluetooth speaker or desktop sound system at low volume — https://maggew.com/random-SciFiAmbience

This shouldn’t take you too long, maybe 10 minutes to get up and running for something that can pay serious dividends to future you.

  1. Download addon to Video Extract URLs — https://chromewebstore.google.com/detail/youtube-url-extractor/jmilibpbdpajjnabchfpfmmmjgbimefo
  2. Visit favorite YouTube channel and scroll to very bottom of videos page so you can “fetch” all the URLS instead of just the top 10 or 20 that load
    1. https://www.youtube.com/@Futurescapes-SciFiAmbience/videos
  3. Click Extract URLs than Copy URLs to clipboard
wordpress redirecting random youtube video (2)
extract all video links from a specific youtube channel and click copy
  1. Visit an online tool to add a prefix and suffix… we want to prepend a quote symbol and append a quote symbol with a comma… I like this one — https://text-tools.dev/prefix-suffix-lines
wordpress redirecting random youtube video (1)
paste all video links into a tool to match code syntax
  1. After you copy to clipboard, we’ll want to create a new snippet than paste and replace the lines below in the code example
  2. Log into WordPress website and Add new snippet than copy / paste the text below and activate.
  3. Visit yourdomain.com/future-SciFiAmbience
  4. *(optional) change line #4 random_slug to whatever you want
  5. *(optional) update your blank new tab landing page or create a bookmark for you visit when you wanna vibe out to some jams… 
<?php
// Add this snippet to your theme's functions.php or use a plugin like "Code Snippets" to include it.
add_action('init', function() {
    $random_slug = 'random-SciFiAmbience'; // The slug to trigger the random video
    $request_uri = trim($_SERVER['REQUEST_URI'], '/');

    if ($request_uri === $random_slug) {
        // Define the list of YouTube video URLs
        $youtube_videos = [
            "https://www.youtube.com/watch?v=G_-XgbDVUdQ&t=406s",
            "https://www.youtube.com/watch?v=_OcDW4p7kHo",
            "https://www.youtube.com/watch?v=VTrMU3WS-lY&t=1s",
            "https://www.youtube.com/watch?v=kiIW2tSfSrk",
            "https://www.youtube.com/watch?v=7sju0dQbkJc",
            // Add the rest of your URLs here...
            "https://www.youtube.com/watch?v=HRu2HVLP5iA"
        ];

        // Choose a random video from the list
        $random_video = $youtube_videos[array_rand($youtube_videos)];

        // Redirect to the random video
        wp_redirect($random_video);
        exit;
    }
});

People also search:

  • Easily Add Random YouTube Video Links to Your WordPress Site
  • Simple PHP Code to Show Random YouTube Videos in WordPress
  • Create a Random YouTube Video Redirect in WordPress with This Snippet

1 thought on “HOWTO: Redirect to a Random YouTube Video on WordPress Website”

  1. double-check and make sure all exported URLS from your favorite channel do not have the “&t=406s” at the end or when you visit a link it’ll not start fro the beginning…

    Reply

Leave a Comment

Item added to cart.
0 items - $0