• 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

Jake Bunce

XenForo moderator
Staff member
#2
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

Well-known member
#3
@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.
 
#6
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
 
#8
@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($value, 1);
        }

        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:



and save it. This should work, if you have any questions PM me.