Community Forums › Forums › Archived Forums › Design Tips and Tricks › Home Page Redirect
- This topic has 12 replies, 3 voices, and was last updated 6 years, 5 months ago by Victor Font.
-
AuthorPosts
-
November 6, 2017 at 10:02 am #213316futurewebbossMember
I have some code I'd like to insert so that it only runs on the home page when the site is loaded. The code detects the type of device being used and if it is a mobile device it will redirect to download a progressive mobile web app. I can't use Genesis Simple Hooks because the code will execute on every page. Does anyone have any suggestions?
Thank You
Dan
November 6, 2017 at 10:05 am #213317Brad DaltonParticipantYou have at least 2 options :
1. Add the code to a home.php or front-page.php file.
2. Add the is_home() or is_front_page() conditional tag to the code.
November 6, 2017 at 10:47 am #213322futurewebbossMemberHi Brad,
Thanks. Here's the full code. Not sure how or where to add what you're specifying. I was reading about these but not sure how to implement.
Thanks for the reply.
Dan
________________________________________________________________________________________<script>
var app_url = "https://customers.mappaquerque.com/m/masterkelly/?appcode=masterkelly";
app_url = app_url + "&goBack=true";
var ba_is_mobile_browser = (navigator.userAgent.match(/iPhone/i)||navigator.userAgent.match(/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|vodafone|o2|pocket|kindle|mobile|pda|psp|treo)/i)||navigator.userAgent.match(/iPod/i)||navigator.userAgent.match(/operamini/i)||navigator.userAgent.match(/blackberry/i)||navigator.userAgent.match(/(palmos|palm|hiptop|avantgo|plucker|xiino|blazer|elaine)/i)||navigator.userAgent.match(/(windowsce; ppc;|windows ce; smartphone;|windows ce;iemobile)/i)||navigator.userAgent.match(/android/i))&&!navigator.userAgent.match(/iPad/i);var queryParams = getQueryParams(document.location.search);
if(queryParams && (queryParams.setDesktopCookie == 1)) {
//if there's a query param to set cookie set it (since we can't set them on another domain for this domain)
setCookie("viewmode","desktop");
}var ba_desktop_cookie = getCookie("viewmode");
var ba_is_mobile = ba_is_mobile_browser && (ba_desktop_cookie != "desktop");function redirectToApp() {
window.location = app_url;
}function getQueryParams(qs) {
qs = qs.split("+").join(" ");var params = {}, tokens,
re = /[?&]?([^=]+)=([^&]*)/g;while (tokens = re.exec(qs)) {
params[decodeURIComponent(tokens[1])]
= decodeURIComponent(tokens[2]);
}return params;
}function getCookie(name) {
var re = new RegExp(name + "=([^;]+)");
var value = re.exec(document.cookie);
return (value != null) ? unescape(value[1]) : null;
}function setCookie(key,value) {
document.cookie = key+"="+value;
}if (ba_is_mobile) {
var viewmodeCookie = getCookie("viewmode");
if(viewmodeCookie == null) {
//if it's mobile and there's no cookie set, redirect to mobile site
redirectToApp();
}
}//create the html elements on the page
var ba_view_elem = document.createElement("SPAN");
var ba_view_text = document.createTextNode("View in ");
ba_view_elem.appendChild(ba_view_text);var ba_mobile_elem = document.createElement("A");
ba_mobile_elem.setAttribute("onclick", "redirectToApp()");
ba_mobile_elem.setAttribute("href", "javascript:;");
var ba_mobile_text = document.createTextNode("Mobile");
ba_mobile_elem.appendChild(ba_mobile_text);
ba_view_elem.appendChild(ba_mobile_elem);var ba_view_text = document.createTextNode(" | ");
ba_view_elem.appendChild(ba_view_text);var ba_desktop_elem = document.createElement("B");
var ba_desktop_text = document.createTextNode("Desktop");
ba_desktop_elem.appendChild(ba_desktop_text);ba_view_elem.appendChild(ba_desktop_elem);
var ba_curTag = document.currentScript
var ba_parent_elem = ba_curTag.parentNode;
ba_parent_elem.insertBefore(ba_view_elem, ba_curTag);
</script>November 6, 2017 at 10:55 am #213324Brad DaltonParticipantTry adding it to your home page file. You'll need to wrap it in closing and opening PHP tags.
?> <script>Your Script</script> <?php
November 6, 2017 at 11:31 am #213327futurewebbossMemberGive up for now. Cant' get it to load without errors.
November 6, 2017 at 11:39 am #213328futurewebbossMemberThis is what I ended up with that didn't work. Gave me a php error on load
Parse error: syntax error, unexpected '<' in /home/hiptolik/public_html/index.php on line 3
______________________________________________________________________<?php
if ( is_home() ):
<script>
var app_url = "https://customers.mappaquerque.com/m/masterkelly/?appcode=masterkelly";
app_url = app_url + "&goBack=true";
var ba_is_mobile_browser = (navigator.userAgent.match(/iPhone/i)||navigator.userAgent.match(/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|vodafone|o2|pocket|kindle|mobile|pda|psp|treo)/i)||navigator.userAgent.match(/iPod/i)||navigator.userAgent.match(/operamini/i)||navigator.userAgent.match(/blackberry/i)||navigator.userAgent.match(/(palmos|palm|hiptop|avantgo|plucker|xiino|blazer|elaine)/i)||navigator.userAgent.match(/(windowsce; ppc;|windows ce; smartphone;|windows ce;iemobile)/i)||navigator.userAgent.match(/android/i))&&!navigator.userAgent.match(/iPad/i);var queryParams = getQueryParams(document.location.search);
if(queryParams && (queryParams.setDesktopCookie == 1)) {
//if there's a query param to set cookie set it (since we can't set them on another domain for this domain)
setCookie("viewmode","desktop");
}var ba_desktop_cookie = getCookie("viewmode");
var ba_is_mobile = ba_is_mobile_browser && (ba_desktop_cookie != "desktop");function redirectToApp() {
window.location = app_url;
}function getQueryParams(qs) {
qs = qs.split("+").join(" ");var params = {}, tokens,
re = /[?&]?([^=]+)=([^&]*)/g;while (tokens = re.exec(qs)) {
params[decodeURIComponent(tokens[1])]
= decodeURIComponent(tokens[2]);
}return params;
}function getCookie(name) {
var re = new RegExp(name + "=([^;]+)");
var value = re.exec(document.cookie);
return (value != null) ? unescape(value[1]) : null;
}function setCookie(key,value) {
document.cookie = key+"="+value;
}if (ba_is_mobile) {
var viewmodeCookie = getCookie("viewmode");
if(viewmodeCookie == null) {
//if it's mobile and there's no cookie set, redirect to mobile site
redirectToApp();
}
}//create the html elements on the page
var ba_view_elem = document.createElement("SPAN");
var ba_view_text = document.createTextNode("View in ");
ba_view_elem.appendChild(ba_view_text);var ba_mobile_elem = document.createElement("A");
ba_mobile_elem.setAttribute("onclick", "redirectToApp()");
ba_mobile_elem.setAttribute("href", "javascript:;");
var ba_mobile_text = document.createTextNode("Mobile");
ba_mobile_elem.appendChild(ba_mobile_text);
ba_view_elem.appendChild(ba_mobile_elem);var ba_view_text = document.createTextNode(" | ");
ba_view_elem.appendChild(ba_view_text);var ba_desktop_elem = document.createElement("B");
var ba_desktop_text = document.createTextNode("Desktop");
ba_desktop_elem.appendChild(ba_desktop_text);ba_view_elem.appendChild(ba_desktop_elem);
var ba_curTag = document.currentScript
var ba_parent_elem = ba_curTag.parentNode;
ba_parent_elem.insertBefore(ba_view_elem, ba_curTag);
</script>November 6, 2017 at 12:22 pm #213330Brad DaltonParticipantYou don't need a conditional tag when adding the code directly to a home or front page template file.
November 6, 2017 at 2:50 pm #213334futurewebbossMemberStill no go.
Parse error: syntax error, unexpected '>' in /home/hiptolik/public_html/index.php on line 3
<?php
(is_home()
<script>
var app_url = "https://customers.mappaquerque.com/m/masterkelly/?appcode=masterkelly";
app_url = app_url + "&goBack=true";
var ba_is_mobile_browser = (navigator.userAgent.match(/iPhone/i)||navigator.userAgent.match(/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|vodafone|o2|pocket|kindle|mobile|pda|psp|treo)/i)||navigator.userAgent.match(/iPod/i)||navigator.userAgent.match(/operamini/i)||navigator.userAgent.match(/blackberry/i)||navigator.userAgent.match(/(palmos|palm|hiptop|avantgo|plucker|xiino|blazer|elaine)/i)||navigator.userAgent.match(/(windowsce; ppc;|windows ce; smartphone;|windows ce;iemobile)/i)||navigator.userAgent.match(/android/i))&&!navigator.userAgent.match(/iPad/i);var queryParams = getQueryParams(document.location.search);
if(queryParams && (queryParams.setDesktopCookie == 1)) {
//if there's a query param to set cookie set it (since we can't set them on another domain for this domain)
setCookie("viewmode","desktop");
}var ba_desktop_cookie = getCookie("viewmode");
var ba_is_mobile = ba_is_mobile_browser && (ba_desktop_cookie != "desktop");function redirectToApp() {
window.location = app_url;
}function getQueryParams(qs) {
qs = qs.split("+").join(" ");var params = {}, tokens,
re = /[?&]?([^=]+)=([^&]*)/g;while (tokens = re.exec(qs)) {
params[decodeURIComponent(tokens[1])]
= decodeURIComponent(tokens[2]);
}return params;
}function getCookie(name) {
var re = new RegExp(name + "=([^;]+)");
var value = re.exec(document.cookie);
return (value != null) ? unescape(value[1]) : null;
}function setCookie(key,value) {
document.cookie = key+"="+value;
}if (ba_is_mobile) {
var viewmodeCookie = getCookie("viewmode");
if(viewmodeCookie == null) {
//if it's mobile and there's no cookie set, redirect to mobile site
redirectToApp();
}
}//create the html elements on the page
var ba_view_elem = document.createElement("SPAN");
var ba_view_text = document.createTextNode("View in ");
ba_view_elem.appendChild(ba_view_text);var ba_mobile_elem = document.createElement("A");
ba_mobile_elem.setAttribute("onclick", "redirectToApp()");
ba_mobile_elem.setAttribute("href", "javascript:;");
var ba_mobile_text = document.createTextNode("Mobile");
ba_mobile_elem.appendChild(ba_mobile_text);
ba_view_elem.appendChild(ba_mobile_elem);var ba_view_text = document.createTextNode(" | ");
ba_view_elem.appendChild(ba_view_text);var ba_desktop_elem = document.createElement("B");
var ba_desktop_text = document.createTextNode("Desktop");
ba_desktop_elem.appendChild(ba_desktop_text);ba_view_elem.appendChild(ba_desktop_elem);
var ba_curTag = document.currentScript
var ba_parent_elem = ba_curTag.parentNode;
ba_parent_elem.insertBefore(ba_view_elem, ba_curTag);
</script>
<?php
/**
* Front to the WordPress application. This file doesn't do anything, but loads
* wp-blog-header.php which does and tells WordPress to load the theme.
*
* @package WordPress
*//**
* Tells WordPress to load the WordPress theme and output it.
*
* @var bool
*/
define('WP_USE_THEMES', true);/** Loads the WordPress Environment and Template */
require( dirname( __FILE__ ) . '/wp-blog-header.php' );November 6, 2017 at 4:19 pm #213335Victor FontModeratorThe problem you are having is because PHP is trying to execute the jQuery. When you include scripts conditionally, you either need to wrap only the conditional in PHP tags or echo the script within PHP. To do the first, you need something like this:
<?php if ( is_home() ) { ?> <script> ... </script> <?php } ?>
For the second, use something like this:
<?php if ( is_home() ) { echo "<script>...</script>"; } ?>
Regards,
Victor
https://victorfont.com/
Call us toll free: 844-VIC-FONT (842-3668)
Have you requested your free website audit yet?November 6, 2017 at 8:06 pm #213342futurewebbossMemberHi Victor,
Still getting Fatal error: Call to undefined function is_home() in /home/hiptolik/public_html/index.php on line 2
Should I be putting this somewhere else besides index.php?
HEre's the code
______________________________________________________________________________
<?php
if ( is_home() ) {
?>
<script>
var app_url = "https://customers.mappaquerque.com/m/masterkelly/?appcode=masterkelly";
app_url = app_url + "&goBack=true";
var ba_is_mobile_browser = (navigator.userAgent.match(/iPhone/i)||navigator.userAgent.match(/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|vodafone|o2|pocket|kindle|mobile|pda|psp|treo)/i)||navigator.userAgent.match(/iPod/i)||navigator.userAgent.match(/operamini/i)||navigator.userAgent.match(/blackberry/i)||navigator.userAgent.match(/(palmos|palm|hiptop|avantgo|plucker|xiino|blazer|elaine)/i)||navigator.userAgent.match(/(windowsce; ppc;|windows ce; smartphone;|windows ce;iemobile)/i)||navigator.userAgent.match(/android/i))&&!navigator.userAgent.match(/iPad/i);var queryParams = getQueryParams(document.location.search);
if(queryParams && (queryParams.setDesktopCookie == 1)) {
//if there's a query param to set cookie set it (since we can't set them on another domain for this domain)
setCookie("viewmode","desktop");
}var ba_desktop_cookie = getCookie("viewmode");
var ba_is_mobile = ba_is_mobile_browser && (ba_desktop_cookie != "desktop");function redirectToApp() {
window.location = app_url;
}function getQueryParams(qs) {
qs = qs.split("+").join(" ");var params = {}, tokens,
re = /[?&]?([^=]+)=([^&]*)/g;while (tokens = re.exec(qs)) {
params[decodeURIComponent(tokens[1])]
= decodeURIComponent(tokens[2]);
}return params;
}function getCookie(name) {
var re = new RegExp(name + "=([^;]+)");
var value = re.exec(document.cookie);
return (value != null) ? unescape(value[1]) : null;
}function setCookie(key,value) {
document.cookie = key+"="+value;
}if (ba_is_mobile) {
var viewmodeCookie = getCookie("viewmode");
if(viewmodeCookie == null) {
//if it's mobile and there's no cookie set, redirect to mobile site
redirectToApp();
}
}//create the html elements on the page
var ba_view_elem = document.createElement("SPAN");
var ba_view_text = document.createTextNode("View in ");
ba_view_elem.appendChild(ba_view_text);var ba_mobile_elem = document.createElement("A");
ba_mobile_elem.setAttribute("onclick", "redirectToApp()");
ba_mobile_elem.setAttribute("href", "javascript:;");
var ba_mobile_text = document.createTextNode("Mobile");
ba_mobile_elem.appendChild(ba_mobile_text);
ba_view_elem.appendChild(ba_mobile_elem);var ba_view_text = document.createTextNode(" | ");
ba_view_elem.appendChild(ba_view_text);var ba_desktop_elem = document.createElement("B");
var ba_desktop_text = document.createTextNode("Desktop");
ba_desktop_elem.appendChild(ba_desktop_text);ba_view_elem.appendChild(ba_desktop_elem);
var ba_curTag = document.currentScript
var ba_parent_elem = ba_curTag.parentNode;
ba_parent_elem.insertBefore(ba_view_elem, ba_curTag);
</script>
<?php
}
?>
<?php
/**
* Front to the WordPress application. This file doesn't do anything, but loads
* wp-blog-header.php which does and tells WordPress to load the theme.
*
* @package WordPress
*//**
* Tells WordPress to load the WordPress theme and output it.
*
* @var bool
*/
define('WP_USE_THEMES', true);/** Loads the WordPress Environment and Template */
require( dirname( __FILE__ ) . '/wp-blog-header.php' );November 6, 2017 at 8:38 pm #213343futurewebbossMemberIndex.php is obviously not the place for this code. I added the code to genesis simple hooks to execute on the before closing head and am testing that now. You guys were probably assuming such and I'm sorry for wasting anyones time here. Will let you know how it works out.
November 6, 2017 at 8:42 pm #213344futurewebbossMemberOk I put the code (below) in Genesis wp_head Simple hooks and set to execute php at the site dennis-kelly.com and it appears to work flawlessly
-------------------------------------------------------------
<?php
if ( is_home() ) {
?>
<script>
var app_url = "https://customers.mappaquerque.com/m/masterkelly/?appcode=masterkelly";
app_url = app_url + "&goBack=true";
var ba_is_mobile_browser = (navigator.userAgent.match(/iPhone/i)||navigator.userAgent.match(/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|vodafone|o2|pocket|kindle|mobile|pda|psp|treo)/i)||navigator.userAgent.match(/iPod/i)||navigator.userAgent.match(/operamini/i)||navigator.userAgent.match(/blackberry/i)||navigator.userAgent.match(/(palmos|palm|hiptop|avantgo|plucker|xiino|blazer|elaine)/i)||navigator.userAgent.match(/(windowsce; ppc;|windows ce; smartphone;|windows ce;iemobile)/i)||navigator.userAgent.match(/android/i))&&!navigator.userAgent.match(/iPad/i);var queryParams = getQueryParams(document.location.search);
if(queryParams && (queryParams.setDesktopCookie == 1)) {
//if there's a query param to set cookie set it (since we can't set them on another domain for this domain)
setCookie("viewmode","desktop");
}var ba_desktop_cookie = getCookie("viewmode");
var ba_is_mobile = ba_is_mobile_browser && (ba_desktop_cookie != "desktop");function redirectToApp() {
window.location = app_url;
}function getQueryParams(qs) {
qs = qs.split("+").join(" ");var params = {}, tokens,
re = /[?&]?([^=]+)=([^&]*)/g;while (tokens = re.exec(qs)) {
params[decodeURIComponent(tokens[1])]
= decodeURIComponent(tokens[2]);
}return params;
}function getCookie(name) {
var re = new RegExp(name + "=([^;]+)");
var value = re.exec(document.cookie);
return (value != null) ? unescape(value[1]) : null;
}function setCookie(key,value) {
document.cookie = key+"="+value;
}if (ba_is_mobile) {
var viewmodeCookie = getCookie("viewmode");
if(viewmodeCookie == null) {
//if it's mobile and there's no cookie set, redirect to mobile site
redirectToApp();
}
}//create the html elements on the page
var ba_view_elem = document.createElement("SPAN");
var ba_view_text = document.createTextNode("View in ");
ba_view_elem.appendChild(ba_view_text);var ba_mobile_elem = document.createElement("A");
ba_mobile_elem.setAttribute("onclick", "redirectToApp()");
ba_mobile_elem.setAttribute("href", "javascript:;");
var ba_mobile_text = document.createTextNode("Mobile");
ba_mobile_elem.appendChild(ba_mobile_text);
ba_view_elem.appendChild(ba_mobile_elem);var ba_view_text = document.createTextNode(" | ");
ba_view_elem.appendChild(ba_view_text);var ba_desktop_elem = document.createElement("B");
var ba_desktop_text = document.createTextNode("Desktop");
ba_desktop_elem.appendChild(ba_desktop_text);ba_view_elem.appendChild(ba_desktop_elem);
var ba_curTag = document.currentScript
var ba_parent_elem = ba_curTag.parentNode;
ba_parent_elem.insertBefore(ba_view_elem, ba_curTag);
</script>
<?php
}
?>November 7, 2017 at 3:00 am #213348Victor FontModeratorAs Brad said earlier, you need to add the code to your home page file, which is either front-page.php or home.php. Index.php has nothing to do with the home page. WordPress isn't even loaded yet when index.php is called. That's why is_home() isn't working. is_home() is a WordPress conditional.
If you hire a developer, this could be done for you in a few minutes.
Regards,
Victor
https://victorfont.com/
Call us toll free: 844-VIC-FONT (842-3668)
Have you requested your free website audit yet? -
AuthorPosts
- The forum ‘Design Tips and Tricks’ is closed to new topics and replies.