• Skip to main content
  • Skip to forum navigation

StudioPress

  • Shop for Themes
  • My StudioPress

Forum navigation

  • Home
  • General Genesis Discussions
  • StudioPress Themes
  • Genesis Blocks
    • Genesis Blocks
    • Genesis Custom Blocks
  • Retired Themes
  • FAQs
  • Forum Rules
  • Internationalization and Translations
  • Forum Bugs and Suggestions
  • Forum Log In

Are You Using The WordPress Block Editor?

Genesis now offers plugins that help you build better sites faster with the WordPress block editor (Gutenberg). Try the feature-rich free versions of each plugin for yourself!

Genesis Blocks Genesis Custom Blocks

Home Page Redirect

Welcome!

These forums are for general discussion on WordPress and Genesis. Official support for StudioPress themes is offered exclusively at My StudioPress. Responses in this forum are not guaranteed. Please note that this forum will require a new username, separate from the one used for My.StudioPress.

Log In
Register Lost Password

Community Forums › Forums › Archived Forums › Design Tips and Tricks › Home Page Redirect

This topic is: not resolved

Tagged: home page, redirect

  • This topic has 12 replies, 3 voices, and was last updated 5 years, 4 months ago by Victor Font.
Viewing 13 posts - 1 through 13 (of 13 total)
  • Author
    Posts
  • November 6, 2017 at 10:02 am #213316
    futurewebboss
    Member

    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 #213317
    Brad Dalton
    Participant

    You 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.


    Tutorials for StudioPress Themes.

    November 6, 2017 at 10:47 am #213322
    futurewebboss
    Member

    Hi 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 #213324
    Brad Dalton
    Participant

    Try adding it to your home page file. You'll need to wrap it in closing and opening PHP tags.

    ?>
    <script>Your Script</script>
    <?php
    

    Tutorials for StudioPress Themes.

    November 6, 2017 at 11:31 am #213327
    futurewebboss
    Member

    Give up for now. Cant' get it to load without errors.

    November 6, 2017 at 11:39 am #213328
    futurewebboss
    Member

    This 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 #213330
    Brad Dalton
    Participant

    You don't need a conditional tag when adding the code directly to a home or front page template file.


    Tutorials for StudioPress Themes.

    November 6, 2017 at 2:50 pm #213334
    futurewebboss
    Member

    Still 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 #213335
    Victor Font
    Moderator

    The 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 #213342
    futurewebboss
    Member

    Hi 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 #213343
    futurewebboss
    Member

    Index.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 #213344
    futurewebboss
    Member

    Ok 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 #213348
    Victor Font
    Moderator

    As 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?

  • Author
    Posts
Viewing 13 posts - 1 through 13 (of 13 total)
  • The forum ‘Design Tips and Tricks’ is closed to new topics and replies.

CTA

Ready to get started? Create a site or shop for themes.

Create a site with WP EngineShop for Themes

Footer

StudioPress

© 2023 WPEngine, Inc.

Products
  • Create a Site with WP Engine
  • Shop for Themes
  • Theme Features
  • Get Started
  • Showcase
Company
  • Brand Assets
  • Terms of Service
  • Accptable Usse Policy
  • Privacy Policy
  • Refund Policy
  • Contact Us
Community
  • Find Developers
  • Forums
  • Facebook Group
  • #GenesisWP
  • Showcase
Resources
  • StudioPress Blog
  • Help & Documentation
  • FAQs
  • Code Snippets
  • Affiliates
Connect
  • StudioPress Live
  • StudioPress FM
  • Facebook
  • Twitter
  • Dribbble