Ask the user who they are and show them the relevant content

Hi there,

I would like to ask user following questions:

1. Who they are: If they are a student, working professional or an employer?
2. Where they are based?
3. What they want help with?
4. Which language they want to browse the website in?

Based on the answers, I want to redirect them to a specific content.

Next time, the user visits the website, I want to remember those settings and take them to the same place.

Any ideas, how this can be achieved?

Kind regards,
Malkiat Singh

  • Predrag Dubajic

    Hey Malkiat,

    Hope you're doing well today :slight_smile:

    This could be possible with combining some plugins and custom functions, for start you will need something like this plugin https://wordpress.org/plugins/user-meta/ to add custom fields you listed to your registration form.

    Combining those custom fields with a function like the one below should do the trick:

    <?php
    /**
     * WordPress function for redirecting users based on custom user meta
     */
    function my_login_redirect( $url, $request, $user ){
        if( $user && is_object( $user ) && is_a( $user, 'WP_User' ) ) {
            if( 'cool' == get_user_meta( $user->ID, '_is_cool', true ) ) {
                $url = home_url('/cool-people-only/');
            }
        }
        return $url;
    }
    add_filter('login_redirect', 'my_login_redirect', 10, 3 );

    You can read more about that here:
    http://wpscholar.com/blog/wordpress-user-login-redirect/

    Hope this helps :slight_smile:

    Best regards,
    Predrag

  • Adam Czajczyk

    Hello Malkiat,

    Thanks for clarification, that changes things a bit but fortunately not that much :slight_smile:

    First step would be to create a form and place it on your site. I'd go with Contact Form 7 plugin as it's free and provides an "Additional Settings" section where you could add some Java Script.

    1. That said, first let's create a new Contact Form 7 form:

    - go to "Contact -> Add new" page, click on add new and give a title to the form
    - based on plugin's docs create your form's content here; for example I've added this markup`

    <p>Who are you? (required)<br />
    [text* occupation] </p>

    <p>[submit "Send"]</p>`

    This will generate a simple form including only a text filed and a submit button.

    - switch to "Additional settings" tab and add this line:

    demo_mode: on

    This will prevent form from being send to you by e-mail (we probably don't want that)

    - save form
    - copy form's shortcode that will show up
    - create new page and add that shortcode there; publish post

    Now you should have you for ready and working. To see it just visit that newly created post.

    2. Now we need some code that will check values of the form fields and perform redirect; here we'll call this code

    - go back to "Contact -> Contact Forms -> Additional Settings" page
    - add another line there:

    on_sent_ok: "wpmu_simple_form_redirection()"

    - save form

    3. Let's add actual "check and redirect" code

    - add this code to your current theme's "functions.php" file:

    function wpmu_simple_form_redirect() {
    	?>
    	<script type="text/javascript">
    	function wpmu_simple_form_redirection() {
    		occupation = document.getElementsByName("occupation")[0].value;
    
    		if (occupation == 'student') window.location = "https://google.com";
    		if (occupation == 'employer') window.location = "https://bing.com";
    	}
    	</script>
    	<?php
    }
    add_action('wp_footer','wpmu_simple_form_redirect');

    At this point you've got a page with a form where user can answer the question "Who are you?".
    If he writes a word "student" he's redirected to "https//google.com", if he writes "employer" he's redirected to "https://bing.com".

    Summary:

    1. You can now add another fields to your form ("Contact -> Contact Forms -> [edit selected form]" in your dashboard. Here's a full documentation on the fileds:

    http://contactform7.com/docs/

    2. While adding fields to form (regardless whether these are "text" or "select" or any other type of fileds) make sure that you've gave them unique "name" parameter.

    3. Use this parameter in the last ("check and redirect") code I shared, the one in your functions.php file. You can duplicate the

    occupation = document.getElementsByName("occupation")[0].value;

    line as many times as many fields are in your form. For every field substitute both "occupation" occurrences inside this line with a real "name" that you gave to the filed.

    4. Inside the same code replicate also this line:

    if (occupation == 'student') window.location = "https://google.com";

    Replace "occupation" with a name of a filed you'd like to check, replace "student" with expected value (one value per line) and replace "https://google.com" with redirect target.

    I hope that helps!

    Best regards,
    Adam

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.