Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2011-05-16 17:02:12

philwareham
Core designer
From: Haslemere, Surrey, UK
Registered: 2009-06-11
Posts: 3,564
Website GitHub Mastodon

Help with <txp:php> tags

I need to replicate the following working code in a TXP page…

<?php
function is_facebook(){
if(!(stristr($_SERVER["HTTP_USER_AGENT"],'facebook') === FALSE))
return true;
}?>
<!doctype html>
<head>
<meta charset="utf-8">
<title></title>
<meta name="description" content="a description">
<?php if(is_facebook()){?>
<meta property="og:title" content="">
<meta property="og:description" content="">
<meta property="og:type" content="">
<meta property="og:image" content="">
<meta property="og:url" content="">
<meta property="og:site_name" content="">
<meta property="fb:appid" content="">
<?php }?>

And my non-working TXP code is…

<txp:php>
function is_facebook(){
if(!(stristr($_SERVER["HTTP_USER_AGENT"],'facebook') === FALSE))
return true;
}</txp:php>
<!doctype html>
<head>
<meta charset="utf-8">
<title></title>
<meta name="description" content="a description">
<txp:php> if(is_facebook()){</txp:php>
<meta property="og:title" content=""> 
<meta property="og:description" content="">
<meta property="og:type" content="">
<meta property="og:image" content="">
<meta property="og:url" content="">
<meta property="og:site_name" content="">
<meta property="fb:appid" content="">
<txp:php> }</txp:php>

Any idea why that does not work?

By the way, if you’re wondering, it’s a method to get Facebook’s horrible Open Graph Protocol to validate in HTML5.

Last edited by philwareham (2011-05-16 17:03:48)

Offline

#2 2011-05-16 17:19:28

ruud
Developer Emeritus
From: a galaxy far far away
Registered: 2006-06-04
Posts: 5,068
Website

Re: Help with <txp:php> tags

Read this FAQ page, specifically the part about variable scope and you’ll end up with something like this:

<!doctype html>
<head>
<meta charset="utf-8">
<title></title>
<meta name="description" content="a description">
<txp:php>
if(!(stristr($_SERVER["HTTP_USER_AGENT"],'facebook') === FALSE)){
echo '<meta property="og:title" content=""> 
<meta property="og:description" content="">
<meta property="og:type" content="">
<meta property="og:image" content="">
<meta property="og:url" content="">
<meta property="og:site_name" content="">
<meta property="fb:appid" content="">';
}</txp:php>

Offline

#3 2011-05-16 17:22:08

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,944
Website GitHub

Re: Help with <txp:php> tags

Ruud beat me to it with an inline solution. This version is an alternative, setting a TXP variable inside your PHP and then using <txp:if_variable>:

<txp:php>
global $variable;
$variable['is_facebook'] = (stristr(serverSet('HTTP_USER_AGENT'), 'facebook') === FALSE);
</txp:php>

<!doctype html>
<head>
<meta charset="utf-8">
<title></title>
<meta name="description" content="a description">
<txp:if_variable name="is_facebook" value="">
<meta property="og:title" content=""> 
<meta property="og:description" content="">
<meta property="og:type" content="">
<meta property="og:image" content="">
<meta property="og:url" content="">
<meta property="og:site_name" content="">
<meta property="fb:appid" content="">
</txp:if_variable>

EDIT: forgot value attribute, and switched logic. Note to self: test first.

Last edited by Bloke (2011-05-16 17:30:22)


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#4 2011-05-16 17:49:13

philwareham
Core designer
From: Haslemere, Surrey, UK
Registered: 2009-06-11
Posts: 3,564
Website GitHub Mastodon

Re: Help with <txp:php> tags

Nice, thanks guys!

Offline

#5 2011-05-16 18:59:37

maniqui
Member
From: Buenos Aires, Argentina
Registered: 2004-10-10
Posts: 3,070
Website

Re: Help with <txp:php> tags

Hi Phil.
Curious: why would you want the output only if the user agent is Facebook? Why not have that OpenGraph metadata there for every other service that may be able to consume it?


La música ideas portará y siempre continuará

TXP Builders – finely-crafted code, design and txp

Offline

#6 2011-05-16 20:58:43

philwareham
Core designer
From: Haslemere, Surrey, UK
Registered: 2009-06-11
Posts: 3,564
Website GitHub Mastodon

Re: Help with <txp:php> tags

@maniqui

No other service of note uses it apart from facebook as far as I know, I’d rather have valid code now and if they update the open graph spec in future I’ll revert to showing to all useragents. Why they use property as an attribute and not name is baffling really.

Offline

#7 2011-05-16 21:17:42

GugUser
Member
From: Quito (Ecuador)
Registered: 2007-12-16
Posts: 1,477

Re: Help with <txp:php> tags

Facebook is a user agent such as a browser? I don’t understand the meaning of this code. Can someone explain it. Thanks.

Offline

#8 2011-05-16 22:30:35

philwareham
Core designer
From: Haslemere, Surrey, UK
Registered: 2009-06-11
Posts: 3,564
Website GitHub Mastodon

Re: Help with <txp:php> tags

Sure guguser, basically when a Facebook user ‘likes’ your page the Facebook robot scrapes the page for meta content. Using the above method you can target content (in this case the open graph meta) at just that robot using it’s useragent string. The content is removed from the page for any other useragent browsing it.

Offline

#9 2011-05-16 22:59:00

GugUser
Member
From: Quito (Ecuador)
Registered: 2007-12-16
Posts: 1,477

Re: Help with <txp:php> tags

Thank you, Phil.

Offline

#10 2011-05-21 16:36:58

paperboy
Member
From: Gothenburg, SE
Registered: 2006-01-24
Posts: 30
Website

Re: Help with <txp:php> tags

I’ve never wanted to add tons of extra meta tags just for facebook so this is a great tip. Thanks!

Offline

Board footer

Powered by FluxBB