• Resolved Alex

    (@alexius94)


    Is there a way for me to change the prefix based on which page the user register from? For example I have two landing page, if the user register from landing page A, the username will be FB(A) – User123.

    Thanks.

    • This topic was modified 8 years, 2 months ago by Alex.
Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Nextendweb

    (@nextendweb)

    Hi @alexius94,
    You should track your user on those pages. For example, you can create a cookie with JavaScript, so you will know later which page was visited by the user.

    Then, Nextend Social Login has a filter where you can modify the email and the username too.

            $userData = array(
                'email'    => $email,
                'username' => $sanitized_user_login
            );
    
            $userData = apply_filters('nsl_' . $this->getId() . '_register_user_data', $userData);

    So your code would be for Facebook:

    add_filter('nsl_facebook_register_user_data', function($userData){
        if(isset($_COOKIE['page']) && $_COOKIE['page'] == 'A'){
            // Note: username already contains the prefix here. So in this case, I replace fb_ with fba_
            // username already validated. Make sure you do modify it to a valid username or do the username validaton here again.
            $userData['username'] = preg_replace('/ˆfb_/', 'fba_', $userData['username']);
        }
        return $userData;
    });

    BTW:
    I suggest you do not change the username based on the page. I think it would be better and simpler to set a user meta based on the source page. In that case, you should create the very same cookie as before. Then use the nsl_register_new_user action:

    add_action('nsl_register_new_user', function($user_id){
        if(isset($_COOKIE['page']) && $_COOKIE['page'] == 'A'){
            update_user_meta( $user_id, 'page', 'A' );
        }
    });

    Then you can add another plugin which allows you to filter by users by meta, for example: https://wordpress.org/plugins/amr-users/

    Ps.: I have not tested the codes, but if you follow the logic, you should be fine.

    • This reply was modified 8 years, 2 months ago by Nextendweb.
    Thread Starter Alex

    (@alexius94)

    Hi, the cookie that I’ve set on the page doesn’t seems to be detected by the function. I think it’s because that after the user clicks on the button, the page will redirect away. Is there a way for me to pass the page as a parameter for the function instead?

    • This reply was modified 8 years, 2 months ago by Alex.
    Plugin Author Nextendweb

    (@nextendweb)

    Hi @alexius94,
    if you set the cookie properly (expiration, path, etc…), you should be able to access its data in PHP.

    I would like to help you find the cause, but I will need the link to both of your pages which sets the page cookie. Could you send them to support@nextendweb.com?

    Plugin Author Nextendweb

    (@nextendweb)

    It turned out that the cookie was not set correctly (path and expiration). To set cookies with JavaScript use the following code:

    function setCookie(name,value,days) {
      var expires = "";
      if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days*24*60*60*1000));
        expires = "; expires=" + date.toUTCString();
      }
      document.cookie = name + "=" + (value || "") + expires + "; path=/";
    }
    setCookie('page', 'FB-App', 7);
    Thread Starter Alex

    (@alexius94)

    Thanks! Works like a charm.

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘User Prefix For Certain Page’ is closed to new replies.