Redirect first-time users to landing page

2019-03-22 17:52发布

I have a Wordpress website at example.com and I have a landing page at example.com/landing-page. The landing page has a form for users to fill out to get a free consultation.

Is there a way to install a cookie or something so that when first-time visitors visit example.com, they are redirected to example.com/landing-page. But after they fill out the form on the landing page, the next time they visit example.com, they will see the regular homepage (not be redirected).

3条回答
beautiful°
2楼-- · 2019-03-22 18:33

You could use javascript for this. Make sure that you have jQuery included.

Add the following to your global javascript file that's included on both pages as we'll be using these helper functions on both the homepage and the landing page to check if the cookie exists and also to create it.

<script>
function create_cookie(name, value, days) {
    var expires = "";
    if (days) {
        var date = new Date();
        date.setTime( date.getTime() + (days*24*60*60*1000) );
        expires = "; expires=" + date.toGMTString();
    }
    document.cookie = name + "=" + value + expires + "; path=/";
}

function get_cookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) === ' ') {
            c = c.substring(1, c.length);
        }
        if (c.indexOf(nameEQ) === 0) {
            return c.substring(nameEQ.length, c.length);
        }
    }
    return null;
}
</script>

Include the following script on your homepage which basically checks to see if the cookie 'form_submitted' exists, if not then redirect the user to the landing page.

<script>
// Redirect to landing page if 'form_submitted' cookie does not exist
if ( get_cookie( 'form_submitted' ) === 'undefined' ) {
    window.location.href = "http://xxx.com/landing-page";
}
</script>

And on the landing page, you'll need to add the following javascript which binds a listening event for submit of the form and when it happens it creates the cookie and therefore the next time the user goes to the homepage, they won't be redirected.

<script>
jQuery(document).ready(function($) {

    // Listen for the form submit event
    $('#form-id').on('submit', function(){

        // Create cookie so that the user is no longer redirected
        create_cookie('form_submitted', 'true', 30);

    });

});
</script>

I hope this helps. Good luck! =)

Edit: I've just read comments that were left on your original post whilst I was writing my answer. Just wanted to add that the create_cookie function I've added allows you to pass in the number of days, 30 in my example, before you want the cookie to expire so you could set that quite high to make it last longer.

Obviously there's always the risk with javascript cookies that people clear their browser cookies but there's no other way to track something like this on users that are not logged in. What could be more secure in terms of users not be able to remove it is PHP sessions stored on the server but that's going to be a lot more difficult than this and even that's not bullet proof as user data that you use to associate the user to a session with can change like their IP, browser etc so there's always a risk.

What you could have on the form page is a link saying "I've already completed this form, take me to the main website" which sets the cookie again.

Lastly, you'll notice that my solution is lightweight and includes no unnecessary plugins like $.cookie as I've used javascript in large part, only using jQuery for document ready and binding the submit event.

查看更多
戒情不戒烟
3楼-- · 2019-03-22 18:44

If you need a plugin to set a cookie where user visit your home page (not any other page) i had that same problem before and created a plugin to handle that

Find it on https://wordpress.org/plugins/redirect-to-welcome-or-landing-page

All you need to do is to set the url you need your user to be redirected to then it will handle the cookie creation and the redirection code that so search engines know it is a temporary redirection.

查看更多
做个烂人
4楼-- · 2019-03-22 18:46

If you don't have a lot of time it might be easier to install a plugin and configure it to meet your needs. Try http://wordpress.org/plugins/landing-pages/ and if it's not what your looking for try looking for more plugins.

查看更多
登录 后发表回答