Page redirect after playing a video

WordPress AI Code Robot

Create WordPress code snippets for free without a developer!

{Name (First):7.3} requested the  following code from my WordPress AI Code Generator.

Page redirect after playing a video

The robot generated the following code:

Sure, here is a basic example of a WordPress code snippet that prefixes all functions with 'nm_' and redirects the user after a video has been played.

Please note that the redirecting code is simplified and it's only an example. Usually, video players have their own APIs to work with. Assuming we are using a self-hosted HTML5 video, the code will look like:

“`php
function nm_enqueue_scripts() {
wp_enqueue_script( 'nm_video', get_template_directory_uri() . '/js/nm_video.js', array( 'jquery' ), '1.0.0', true );
}
add_action( 'wp_enqueue_scripts', 'nm_enqueue_script' );

function nm_add_video_and_redirect() {
$output = '<video id="nmVideo" width="320" height="240" controls>
<source src="movie.mp4" type="video/mp4">
<source src="movie.ogg" type="video/ogg">
Your browser does not support the video tag.
</video>';

$output .= '<script type="text/javascript">
jQuery(document).ready(function($) {
var vid = document.getElementById("nmVideo");
vid.onended = function() {
window.location.replace("http://www.yoursite.com/"); //Redirect after video ends
};
});
</script>';

echo $output;
}
add_action( 'wp_footer', 'nm_add_video_and_redirect' );
“`

In this code snippet, the function 'nm_enqueue_scripts' specifies a .js file where video-related scripts could be stored.

The function 'nm_add_video_and_redirect' is hooked to the 'wp_footer' action, which means it will be triggered at the footer of the WordPress site. This function displays a video and adds a piece of JavaScript code that is used to redirect the user after the video has ended.

Replace 'http://www.yoursite.com/' with the URL you want to redirect to.

Replace 'movie.mp4' and 'movie.ogg' with the actual paths of your video files.

This code has to be added to the functions.php file of your child theme or a custom functionality plugin. Also, make sure jQuery is enqueued on your site since it is used in the added JavaScript.

More
CODE SNIPPETS