1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

XF 1.4 Add Custom YouTube Field to Identities Section

Discussion in 'XenForo Questions and Support' started by Amaury, Mar 16, 2015.

  1. Amaury

    Amaury Well-Known Member

  2. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    If you edit the Facebook custom field you will see it uses a PHP Callback. You can refer to that callback in creating a similar callback for Youtube.
     
    Amaury likes this.
  3. Amaury

    Amaury Well-Known Member

    @Jake Bunce

    The Facebook field uses XenForo_Helper_UserField for the class and then verifyFacebook for the method. So would it be a simple matter of just changing the method to verifyYouTube?

    I see Twitter also has a PHP callback with the same class and method, just verifyTwitter instead.
     
  4. Liam W

    Liam W Well-Known Member

    You would have to create the verifyYoutube method, I'm pretty sure one doesn't exist in the core code :)
     
    Amaury likes this.
  5. Amaury

    Amaury Well-Known Member

    And how is that accomplished? :)
     
    creativeforge likes this.
  6. creativeforge

    creativeforge Active Member

    Hi Amaury,

    Were you able to solve this issue?

    I'm stuck on this too, and with the location this info displays. I was expecting to show under the avatar or at least the member card...

    Thank you!

    Andre
     
  7. Amaury

    Amaury Well-Known Member

    We've yet to attempt to create it.
     
  8. Natedanp

    Natedanp New Member

    @Amaury and @creativeforge

    Make your way over to your Xenforo files. Go to "Library > XenForo > Helper" and open the file named "UserField.php" and replace the code with this:

    PHP:
    <?php

    class XenForo_Helper_UserField
    {
        public static function 
    verifyFacebook(array $field, &$value, &$error)
        {
            if (
    preg_match('#facebook\.com/(\#!/)?profile\.php\?id=(?P<id>\d+)#i'$value$match))
            {
                
    $value $match['id'];
            }
            else if (
    preg_match('#facebook\.com/(\#!/)?(?P<id>[a-z0-9\.]+)#i'$value$match))
            {
                if (
    substr($match['id'], -4) != '.php')
                {
                    
    $value $match['id'];
                }
            }

            if (!
    preg_match('/^[a-z0-9\.]+$/i'$value))
            {
                
    $error = new XenForo_Phrase('please_enter_valid_facebook_username_using_alphanumeric_dot_numbers');
                return 
    false;
            }

            return 
    true;
        }
      
        public static function 
    verifyYoutube(array $field, &$value, &$error)
        {
            if (
    preg_match('#youtube\.com/user/(\#!/)?profile\.php\?id=(?P<id>\d+)#i'$value$match))
            {
                
    $value $match['id'];
            }
            else if (
    preg_match('#youtube\.com/user/(\#!/)?(?P<id>[a-z0-9\.]+)#i'$value$match))
            {
                if (
    substr($match['id'], -4) != '.php')
                {
                    
    $value $match['id'];
                }
            }

            if (!
    preg_match('/^[a-z0-9\.]+$/i'$value))
            {
                
    $error = new XenForo_Phrase('please_enter_valid_youtube_username_using_alphanumeric_dot_numbers');
                return 
    false;
            }

            return 
    true;
        }

        public static function 
    verifyTwitter(array $field, &$value, &$error)
        {
            if (
    $value[0] == '@')
            {
                
    $value substr($value1);
            }

            if (!
    preg_match('/^[a-z0-9_]+$/i'$value))
            {
                
    $error = new XenForo_Phrase('please_enter_valid_twitter_name_using_alphanumeric');
                return 
    false;
            }

            return 
    true;
        }
    }
    Or Simply add this after the verifyFacebook code/Inbetween verifyFacebook and verifyTwitter. Just make sure your code looks like the one above this one.

    PHP:
        public static function verifyYoutube(array $field, &$value, &$error)
        {
            if (
    preg_match('#youtube\.com/user/(\#!/)?profile\.php\?id=(?P<id>\d+)#i'$value$match))
            {
                
    $value $match['id'];
            }
            else if (
    preg_match('#youtube\.com/user/(\#!/)?(?P<id>[a-z0-9\.]+)#i'$value$match))
            {
                if (
    substr($match['id'], -4) != '.php')
                {
                    
    $value $match['id'];
                }
            }

            if (!
    preg_match('/^[a-z0-9\.]+$/i'$value))
            {
                
    $error = new XenForo_Phrase('please_enter_valid_youtube_username_using_alphanumeric_dot_numbers');
                return 
    false;
            }

            return 
    true;
        }

    Now unfortunately, I was unable to figure out how to get the

    PHP:
                $error = new XenForo_Phrase('please_enter_valid_youtube_username_using_alphanumeric_dot_numbers');
    code to work, but I'm sure it's an easy fix. Right now, it simply says

    Code:
    The following error occurred:
    
    YouTube Channel:
    please_enter_valid_youtube_username_using_alphanumeric_dot_numbers
    Instead of something like this when you do something wrong, like inserting the wrong link.

    Code:
    The following error occurred:
    
    Facebook:
    Please enter a valid Facebook user name using only a-z, 0-9, and . characters, or a user ID using only numbers.
    

    Apart from that, after you did this and saved it, go to or create the User Field you want for Youtube, edit it and go to the "Options for Text Fields" tab. Click "PHP Callback" and put in "XenForo_Helper_UserField" For the first field and "verifyYoutube" for the second field. Make sure it looks like this:

    [​IMG]

    and save it. This should work, if you have any questions PM me.
     
    Jake Bunce likes this.

Share This Page