Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Finn Pryor
Finn Pryor
1,653 Points

Wordpress Age Verification using Cookies

Hi everyone!

So I'm working on a Wordpress website that has age restricted content on it. So I have created an age verification page template and set it as the home page. Now here's my problem, if someone searches my web page on google (or a search engine like it) it will some times display some of the pages of the website, and if someone clicks on one of the pages, they will skip right by the age verification.

So I've been trying to implement a cookie into the website so that when someone goes to any page on the website, it checks for a cookie, and if that cookie doesn't exist on their computer, it will redirect them to the age verification. Once they are redirected to the age verification, if they agree that they are of age, it will create a cookie so that they can browse the website, then the cookie will expire the next day. Also, if they are not of age, on the age verifier I have made, they will be redirected to a "you're too young" page, and what I'm wanting is there to be a cookie created that says they are too young, so if they try to enter the site again (within a day because the expiration time is a day), they will be brought directly to that "too young" page.

So quick review just incase that was confusing: Client enters website, website checks for cookies:

**of age cookie:**     |               **no cookie:**          |   **too young cookie:**
browse website freely  |      redirected to age verification   | redirect                                                           
                                             |
                                             |
                                             V
                                    **Age verification:**

**of age:**                                  |                          not of age:
browse website freely                        |                      redirected to "too young page"                       
cookie created saying of age                 |                      cookie created saying to young           
expiration: one day                          |                      expiration: one day
                                             |                        try to come back, recognizes
                                             |                 cookie, brought to too young page

1 Answer

Zac Gordon
STAFF
Zac Gordon
Treehouse Guest Teacher

I think that cookies is not really the best approach to this. I would recommend setting it up so that a user must either log in or do some simple registration to the site in order to get in to view content. That way you let WordPress handle all the session functionality. It could also handle a redirect to certain page when other pages are viewed or to maybe even update any page to display a login for if someone isn't logged in to the site.

What method are you using to check age? If it's a simple are you old enough or add your date plugin I think it wouldn't be too difficult to modify or create a plugin to check that the age is appropriate before adding the member.

OR, just use something like this ;p Age Verify Plugin

Finn Pryor
Finn Pryor
1,653 Points

Hi Zac, thanks for the reply!

So on the website that I am making is for a rum company, so we are required by law to verify that whoever is trying to view the website, is of legal drinking age. If you look at any alcohol website, they have an age verifier as soon as you try to view the website. So we're not wanting to require people to register, we just want a quick yes or no to "are you of age?".

Also, for the age verify plugin, we were actually using that for a long time, but my client is just wanting yes and no buttons rather than date dropdowns or a checkbox, he's very picky.

Thanks again. -Finn

P.S. if you want to look at what we have now, you can view it at realmccoyspirits.com