Link to home
Start Free TrialLog in
Avatar of TypingStars
TypingStars

asked on

Wordpress - Make header video autoplay ONLY for home page

I figured out how to get my video to auto play when my Homepage is visited.  Only problem is every other tab also starts it all over again.  Anyway in the header to test for homepage so I can set Autoplay="autoplay" else any other page Autoplay""  ?

I copied my header as headerhome.php, but could not find where the homepage was calling it.  Its not in the css either.

Either method works for me,Thank You
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

There may be a WordPress-specific solution, but that said, the general solution is to use a cookie.  When the page is loaded, look for the cookie.  If it's there skip the video.  If it's not there, set the cookie and play the video.  This article describes the concept in detail, with code examples.
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_3314-How-to-Show-an-Introductory-Web-Page-Once-Using-PHP.html
Avatar of TypingStars
TypingStars

ASKER

This is something I'm totally unfamiliar with.  Having no experience dealing with implementing cookies especially on different platforms I would rather have my Homepage use headerhome.php and the rest of my pages header.php  Would you happen to know how to make only my home page reference the other header?  Thanks
If you're not familiar with HTTP cookies, then it's a WordPress-specific question, so I've added the WordPress Zone to the question page here at E-E.  Hope that helps!
Avatar of Julian Hansen
You could do this with javascript by checking if the page is the home page and initiating a play if it is i.e. remove autoplay from the video.

The question is - do you want this to play only on first visit or every visit to the home page. If it must only play once than a javascript cookie solution is the next step. Either way you can probably solve this without having to touch wordpress other than to add some javascript.

If you have a link that we can look at it will be easier to work out exactly what you need.
Thank you for adding it to the Wordpress zone!
I would like the home page to play it every time.  Some people come back months later and we could have a totally new video for them to see.   OK, you asked for the url- www.typingwiththestars.com 
Right now I switched it back so the visitor has to click the play button.  I would rather just on the homepage if it could play automatically to initiate the understanding of the product, Thank You
This should do the trick
<script>
if (window.location.pathname == '/') {
  var vid = document.querySelector('.headerleft video');
  vid.play();
}</script>

Open in new window

I would give your video an ID just to make it more easily targeted for example
<video id="homepagevid" ...>

Open in new window

And then change the script above to
<script>
if (window.location.pathname == '/') {
  var vid = document.getElementById('homepagevid');
  vid.play();
}</script>

Open in new window

This is the code from my header.php   I don't understand how to modify it to include yours around or within it.  Also if it will still work for simplicity, leave off the ID.  I only have one video file.  I'll try what you suggest next, but since time is an issue to get this working, I thought of a work around where I would make the video and picture part of the Homepage, not the header.  This way it will only be on the homepage and not all the others, Thank You

<!-- Static Header Image -->
    <div class="banner"><div class="headerleft">
<video width="478px" height="268" poster="http://mysite.com/content/upload/2015/typing-lessons-video-image.jpg" controls="controls" autobuffer="" ><source src="http://www.mysite.com/Typing_WithThe_Stars.webm" type="video/webm"><source type="video/mp4" src="http://www.mysite.com/Typing-WithThe-Stars.mp4"></source>Your browser does not support the video tag.</video></div>
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That was Fantastic!!! Thank You
You are welcome.