1

I am creating a website in which Facebook's share and like buttons are attached. I want to know if I can change og:description, og:title, og:author etc. dynamically using PHP?

For example:

<meta property="og:url" content="<?php echo my_url(); ?>"/>
<meta property="og:type" content="website" />
<meta property="og:title" content="<?php echo $random_title; ?>"/>
<meta property="og:author" content="<?php echo $random_author; ?>"/>
<meta property="og:description" content="<?php echo $random_desc; ?>"/>

These description, title, author are different for different posts. So can I do it in this way ? Or is there any other way ?

I have got the error when i debug the URL with https://developers.facebook.com/tools/debug/

 Extraneous Property    Objects of this type do not allow properties named 'og:author'.
Parser Mismatched Metadata  The parser's result for this metadata did not match the input metadata. Likely, this was caused by the data being ordered in an unexpected way, multiple values being given for a property only expecting a single value, or property values for a given property being mismatched. Here are the input properties that were not seen in the parsed result: 'og:author'
  • Have you actually tried to test it before you asked a question? – Eda190 Dec 15 '15 at 17:46
  • I have tried today . But the description , title etc remains the old static one that i have given few days back ! Thats why i have this doubt. – Abhishek Developer Dec 15 '15 at 17:49
  • When the debug tool gives you that error message, then the first thing you should do is validate your HTML. http://validator.w3.org/ – CBroe Dec 15 '15 at 19:44

2 Answers2

1

Yes, sure you can. Facebook only cares about the HTML code (or more specifically, the OpenGraph tags) that is delivered via HTTP by the webserver, not how you generate it server-side.

However, you should escape the data that you render inside the OpenGraph tags to prevent Cross-Site Scripting attacks, for instance using PHP's htmlspecialchars function:

<meta property="og:url" content="<?php echo htmlspecialchars(my_url()); ?>"/>
<meta property="og:type" content="website" />
<meta property="og:title" content="<?php echo htmlspecialchars($random_title); ?>"/>
<meta property="og:author" content="<?php echo htmlspecialchars($random_author); ?>"/>
<meta property="og:description" content="<?php echo htmlspecialchars($random_desc); ?>"/>
Community
  • 1
  • 1
helmbert
  • 35,797
  • 13
  • 82
  • 95
  • Thanks for `htmlspecialchars` function. But i tried changing data dynamically. But when i try to share. The data isnt changing. I have both opengraph Meta tags and usual meta tags like `meta name="description"` `meta name="keywords" ` `meta name="author" ` Will it overwrite my og meta ? – Abhishek Developer Dec 15 '15 at 17:58
  • @AbhishekDeveloper, as mentioned in another answer, Facebook caches the OpenGraph data. This means, once Facebook has crawled your site, their data won't update immediately, even if your original content changes. – helmbert Dec 15 '15 at 18:04
  • My description, title,author will be different with each url. The each url will be 'example.php/first', 'example.php/second' I'm showing the content in example.php but have different url for each data.So its not a problem ? – Abhishek Developer Dec 15 '15 at 18:13
  • Yes, the URL is what matters. It's unimportant if all pages are rendered by the same PHP script, as long as the URLs are different. – helmbert Dec 15 '15 at 19:38
1

Yes, of course you can use dynamic tags, as long as they are generated on the Server. Facebook does not parse JavaScript. If the data is still old, you may have to refresh it in the debugger: https://developers.facebook.com/tools/debug/

Facebook caches Open Graph data, make sure you don´t change the tags on a regular basis for a specific URL. Else you would have to refresh the tags all the time. So, forget about choosing random text whenever another User shares your page. Also, if one URL gets liked a lot of times, the data will get locked and you can´t change it anymore.

Think about it this way: A User likes a specific URL, he likes a specific text and specific Open Graph tags. Now if you would change the data for that specific URL the whole time, wouldn´t that be weird for the User who liked it with the other data earlier? Just a side note though ;)

andyrandy
  • 72,880
  • 8
  • 113
  • 130
  • I have a website something like a newschannel. It has a php page in which the data is changing for each post. Like `example.php/first` `example.php/second` . In each url the data is different. – Abhishek Developer Dec 15 '15 at 18:08
  • well, those are 2 different urls, so there´s no problem. you just should not change the tags for one specific url the whole time, that´s a very bad idea. – andyrandy Dec 15 '15 at 18:10
  • I got some errors when debugged. I have edited the question. Take a a look luschn & helmbert – Abhishek Developer Dec 15 '15 at 18:50
  • well, the url you want to debug would help...hard to say with only the error messages – andyrandy Dec 15 '15 at 19:15