Code to identify browser in WordPress

-1

I need a code that identifies if the browser is Firefox and then runs a block of code and otherwise runs another. The code block is HTML5 pure.

The code is to be used inside Wordpress, so I believe only accept HTML , but if there is no other way it can be in PHP or Javascript .

Brother, I'm using the wordpress module on a website of my own. What I want to do is this: The site is a news portal. In it I put a player of my radio online. The code is as follows:

<iframe src="http://player.srvstm.com/player-barra/19378/000000"frameborder="0" width="100%" height="31"></iframe>

I noticed that this player does not run in Firefox and wanted to put an alternative code just for it in Html5, this one below:

<div id="player-html5" class="players">
<audio id="radios-player" controls="" preload="none" autoplay="" src="http://stm3.srvstm.com:19378/;"></audio>

I thought of using an IF in some language to identify the browser, but I do not know if it is the most viable solution.

The site is: www.planetariosdigital.com.br

    
asked by anonymous 23.08.2016 / 06:12

1 answer

0

Hello, to solve your problem my solution is with javascript. But for my code to work you need to make a small change to your iframe code, I need you to add an id, like this:

Original code:

<iframe src="http://player.srvstm.com/player-barra/19378/000000"frameborder="0" width="100%" height="31"></iframe>

Modified Code:

<iframe id="player" src="http://player.srvstm.com/player-barra/19378/000000"frameborder="0" width="100%" height="31"></iframe>

I need this id to be able to manipulate the player on the page with javascript.

Now you should put this PHP code in the functions.php of Wordpress, so that it adds the javascript code responsible for identifying if the surfer is in firefox.

If yes, it will remove the current player and swap for the html5 player.

PHP function:

 add_action('wp_footer', 'firefox_player_audio_javascript', 100);

function firefox_player_audio_javascript() {
echo "<script>if (navigator.userAgent.toLowerCase().indexOf('firefox') > -1) {
var iframe = document.getElementById('player');
var containeriframe = iframe.parentElement.parentElement.parentElement;
var audio = document.createElement('audio');
audio.id = \"radios-player\";
audio.setAttribute(\"controls\", \"\");
audio.preload = \"none\";
audio.setAttribute(\"autoplay\", \"\");
audio.src = \"http://stm3.srvstm.com:19378/;\";
iframe.remove();
containeriframe.appendChild(audio);
}</script>";
}

Remembering that to access the functions.php you go to:

Wordpress Admin panel → Appearance → Editor

Ontherightside->Youshouldfindthefunctions.phporinPortugueseThemeFunctions

Finallysavethefunctionattheendofthefileandseetheresult:

Anyway hope it works, good luck!

    
26.08.2016 / 15:12