Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
Pages: 1
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
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
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
Re: Help with <txp:php> tags
Nice, thanks guys!
Offline
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?
Offline
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
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
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
Pages: 1