PDA

View Full Version : Adding user profile picture to open graph


SilverBoy
01-03-2017, 06:39 PM
Hi

In my CMS articles I displays the avatars of the author of the article, but because the image is small (60×60 px) Facebook didn't grab it when I share the link to Facebook.

So I think in adding user profile picture (Big size) as a variable to the opengraph array.

The question is ..
How I can get the profile picture in every article?
then how I can add it to opengraph array?

Thanks in advance.

MarkFL
01-03-2017, 06:56 PM
Assuming you have the user's userid in $userid, you can get the URL of their profile pic as follows:

$ppuserinfo = fetch_userinfo($userid, 8);

if ($vbulletin->options['usefileavatar'])
{
$ppuserinfo['profilepicurl'] = $vbulletin->options['profilepicurl'] . '/profilepic' . $ppuserinfo['userid'] . '_' . $ppuserinfo['profilepicrevision'] . '.gif';

if (!file_exists($ppuserinfo['profilepicurl']))
{
$ppuserinfo['profilepicrevision'] = 0;
}
}
else
{
$ppuserinfo['profilepicurl'] = 'image.php?' . $vbulletin->session->vars['sessionurl'] . 'u=' . $ppuserinfo['userid'] . "&dateline=$ppuserinfo[profilepicdateline]&type=profile";
}

$profilepicexists = $ppuserinfo['profilepicrevision'] OR $ppuserinfo['profilepicdateline'];

Now, if the variable $profilepicexists evaluates to true, the URL of the profile pic is in $ppuserinfo['profilepicurl']. :)

SilverBoy
01-03-2017, 06:59 PM
Thank you mark, I will give it a try, but how I can inject the profilepic to the opengraph array?
I tried it manually by adding link to the image in the top of vbcms_content_article template but Facebook ignore it and don't add it to the og:images !!

MarkFL
01-03-2017, 07:05 PM
I don't know what key you wish to use for the $opengraph array, but suppose with wish to use the key "profilepicurl', then you could add:

if ($profilepicexists)
{
$opengraph['profilepicurl'] = $ppuserinfo['profilepicurl'];
}

SilverBoy
01-03-2017, 07:11 PM
I want to use og:image property.

MarkFL
01-03-2017, 07:20 PM
I want to use og:image property.

I don't know what that is...I thought you wanted to store the URL of the profile pic in the specified array. :)

I have negligible experience with the CMS articles feature of vB, but I thought I would be able to give you a leg up on getting the data you want into the array. :D

MarkFL
01-03-2017, 07:23 PM
After a quick google search, it appears you need a meta tag in your template, and you will have to register the URL variable for your template, and your meta tag would then look something like:

<meta property="og:image" content="{vb:raw profilepicurl}" />

SilverBoy
01-03-2017, 07:24 PM
:D
When you share a link in FB, the image that appears as thumbnail comes from this property.
If you open any page in sharing debugger tools you will see all properties that FB uses from your page.
https://developers.facebook.com/tools/debug/sharing/

--------------- Added 1483478739 at 1483478739 ---------------

Stupid Q
How I can register the variable?

--------------- Added 1483479009 at 1483479009 ---------------

$ppuserinfo = fetch_userinfo($userid, 8);
Sorry, #8 here what is mean?

MarkFL
01-03-2017, 07:33 PM
Suppose you have the name of the template to which you wish to send the variable in $template_name, then in your plugin, you could use

if ($profilepicexists)
{
vB_Template::preRegister($template_name, array('profilepicurl' => $ppuserinfo['profilepicurl']));
}

SilverBoy
01-03-2017, 07:39 PM
Here is my plugin, is it right?

Hook: vbcms_article_populate_end(vbulletin cms)
Name: Add profile picture as meta tag
Order: 5
Code:
$ppuserinfo = fetch_userinfo($userid, 8);

if ($vbulletin->options['usefileavatar'])
{
$ppuserinfo['profilepicurl'] = $vbulletin->options['profilepicurl'] . '/profilepic' . $ppuserinfo['userid'] . '_' . $ppuserinfo['profilepicrevision'] . '.gif';

if (!file_exists($ppuserinfo['profilepicurl']))
{
$ppuserinfo['profilepicrevision'] = 0;
}
}
else
{
$ppuserinfo['profilepicurl'] = 'image.php?' . $vbulletin->session->vars['sessionurl'] . 'u=' . $ppuserinfo['userid'] . "&amp;dateline=$ppuserinfo[profilepicdateline]&amp;type=profile";
}

$profilepicexists = $ppuserinfo['profilepicrevision'] OR $ppuserinfo['profilepicdateline'];
if ($profilepicexists)
{
vB_Template::preRegister(vbcms_content_article_pag e, array('profilepicurl' => $ppuserinfo['profilepicurl']));
}

--------------- Added 1483479854 at 1483479854 ---------------

It didn't work !!

Here is what I get in my source code
<meta property="og:image" content="" />

MarkFL
01-03-2017, 07:48 PM
You will need to get the actual userid, it's not going to automatically be in $userid...I just used that as a placeholder...for example, at the plugin hook location "postbit_display_complete" you can get it from $post['userid'], but I don't know what array is going to contain it at your plugin hook location.

Looking at that template, I would first try (at the very top of the plugin):

$userid = $poststarter['userid'];

SilverBoy
01-03-2017, 07:52 PM
authorid is the variable :)

Here is the code I use to show avatar for example
require_once(DIR . '/includes/functions_user.php');
$avatarurl = fetch_avatar_url($view->authorid, true);
if ($avatarurl[0]) {
$avatarurl = $avatarurl[0];
}
/* render template and register variables */

$view->avatarurl = $avatarurl;

--------------- Added 1483480748 at 1483480748 ---------------

I modified the plugin code to this, but without luck !!
$ppuserinfo = fetch_userinfo($authorid, 8);

if ($vbulletin->options['usefileavatar'])
{
$ppuserinfo['profilepicurl'] = $vbulletin->options['profilepicurl'] . '/profilepic' . $ppuserinfo['authorid'] . '_' . $ppuserinfo['profilepicrevision'] . '.gif';

if (!file_exists($ppuserinfo['profilepicurl']))
{
$ppuserinfo['profilepicrevision'] = 0;
}
}
else
{
$ppuserinfo['profilepicurl'] = 'image.php?' . $vbulletin->session->vars['sessionurl'] . 'u=' . $ppuserinfo['authorid'] . "&amp;dateline=$ppuserinfo[profilepicdateline]&amp;type=profile";
}

$profilepicexists = $ppuserinfo['profilepicrevision'] OR $ppuserinfo['profilepicdateline'];
if ($profilepicexists)
{
vB_Template::preRegister('vbcms_content_article_pa ge', array('profilepicurl' => $ppuserinfo['profilepicurl']));
}

--------------- Added 1483492627 at 1483492627 ---------------

I made a lot of tests, but I don't know why the plugin can't see $authorid or $poststarter['userid'] or even $userid.

BTW, if I put {vb:raw authorid} in the template it give me the author id !!

Any suggestions?

--------------- Added 1483538845 at 1483538845 ---------------

No suggestions to how to get the authorid to complete this plugin?

--------------- Added 1483542079 at 1483542079 ---------------

I found it ^_^

$userid = $view->authorid;

solve the things

there is only one problem left, this code
$ppuserinfo['profilepicurl'] = $vbulletin->options['profilepicurl'] . '/profilepic' . $ppuserinfo['userid'] . '_' . $ppuserinfo['profilepicrevision'] . '.gif';

give me wrong image pathlike this

customprofilepics/profilepic762011_2.gif

the right path is
www.mysite.com/forum/customprofilepics/profilepic762011_2.gif

How I can fix this?

Thanks in advance.

MarkFL
01-04-2017, 02:35 PM
Try changing the line:

vB_Template::preRegister('vbcms_content_article_pa ge', array('profilepicurl' => $ppuserinfo['profilepicurl']));

to:

vB_Template::preRegister('vbcms_content_article_pa ge', array('profilepicurl' => $vbulletin->options['bburl'] . '/' . $ppuserinfo['profilepicurl']));

SilverBoy
01-04-2017, 06:34 PM
Thank you Mark it works now.

I tried before inject bburl in this line
$ppuserinfo['profilepicurl'] = $vbulletin->options['profilepicurl'] . '/profilepic' . $ppuserinfo['authorid'] . '_' . $ppuserinfo['profilepicrevision'] . '.gif';

but I couldn't figure it out, but now it works and Facebook grap the picture too ^_^
just note I not use template I added the code to the plugin that I bought from BirdOPery5 (More Open Graph Images Gold) and now I have what I want exactly.

MarkFL
01-04-2017, 06:37 PM
Excellent! Glad to hear you now have it working as you want. :up:

SilverBoy
01-04-2017, 06:39 PM
Thank you for your appreciate help, without it I couldn't get things like I want, thank you more and more.