I recently created a site called TrackYourWeight.co.uk and as outlined here, it serves as a demo site as well as a tool for real people! Building the site, gave me the opportunity to the plugin in the way a web developer / customer would. One feature I found missing that would benefit the plugin was a “Registration Wizard”. Ideally, before allowing people full use of the site, I wanted them to complete a three step sign up process. This process will ensure the user completed their “About You” fields, latest weight and finally their target.
So, using the new [wlt-if] shortcode I started by building a “Registration Wizard” page! The page would have three steps:
- Step 1: Complete “About You” fields.
- Step 2: Set your target weight.
- Step 3: Enter your current weight.
The new [wlt-if] shortcode is a clever and not well publicised shortcode. In essence, it allows you to show and hide content depending on whether a Weight Tracker field has been populated. So using the brief above, I was able to develop a wizard page (paste this into the content section of a WordPress page):
(the above code can be copied from this GistHub page)
The content contains three nested [wlt-if] statements. The first statement ensures Height, Date of Birth, Activity Level and Gender have been specified. If they haven’t, Step 1 is shown of the wizard – the User Settings form. When the page reloads, the second [wlt-if] checks if the user has specified a Target Weight – if not, the target form is displayed. The third and final [wlt-if] checks whether or not a Weight entry has been added – in the event it hasn’t the weight entry form is displayed. Finally, if everything has been completed, a [wlt-progress-bar] is shown!
Of course, this relies on the person staying on the wizard page. They could easily click away and never come back – defeating the point of the Wizard! So, I went onto write a little bit of code to put in my theme’s functions.php:
The code (can be copied from this gist) is simple really. On every page load in the front-end several checks are performed. If it’s deemed necessary the user is redirected back to wizard page. Some notes:
- Line 15 checks the user is not on the wizard page, but they are on a page or post and they are logged in. If this isn’t the case, we ignore the visit and do no further work.
- Line 20 does the hard work, the function ws_ls_shortcode_if_value_exist() checks whether the user is logged in and has entered the relevant fields. If not, they are redirected back to the wizard url.
- Remember to modify line 15 and replace /wizard/ with your page slug e.g /the-slug-of-your-wizard-page/
- Modify line 21 to include the URL to your Wizard page.
If you have any questions, please pop them into the forum! 🙂