Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2006-07-19 17:10:12

jonathanclarke
Member
From: London, England
Registered: 2004-12-28
Posts: 46
Website

Accessing the author global variable via PHP

For some reason I can’t access the author global variable via PHP.

I’m using the following code:

<txp:php>echo $GLOBALS['pretext']['author'];</txp:php><br/><br/>

Which doesn’t return anything.

I’ve also tried:

<txp:php>echo $GLOBALS['thisarticle']['author'];</txp:php><br/><br/>

Which returns this tag_error:

Notice: Undefined index: author on line 1

Does anyone know where I’m going wrong?

Last edited by jonathanclarke (2006-07-19 17:26:20)

Offline

#2 2006-07-19 18:58:01

jonathanclarke
Member
From: London, England
Registered: 2004-12-28
Posts: 46
Website

Re: Accessing the author global variable via PHP

PS. I do have PHP enabled in the advanced settings for Textpattern.

Offline

#3 2006-07-19 19:17:09

Sencer
Archived Developer
From: cgn, de
Registered: 2004-03-23
Posts: 1,803
Website

Re: Accessing the author global variable via PHP

Look at what function author($atts) does (in publish/taghandlers.php).

Offline

#4 2006-07-19 19:34:49

jonathanclarke
Member
From: London, England
Registered: 2004-12-28
Posts: 46
Website

Re: Accessing the author global variable via PHP

You mean this function:

function author($atts) { global $thisarticle; extract(lAtts(array('link' => ''),$atts)); $author_name = get_author_name($thisarticle['authorid']); return (empty($link)) ? $author_name : tag($author_name, 'a', ' href="'.pagelinkurl(array('author'=>$author_name)).'"'); }

?

Offline

#5 2006-07-19 19:35:52

jonathanclarke
Member
From: London, England
Registered: 2004-12-28
Posts: 46
Website

Re: Accessing the author global variable via PHP

Do I need to supply author() with some more information?

Offline

#6 2006-07-20 12:13:08

jonathanclarke
Member
From: London, England
Registered: 2004-12-28
Posts: 46
Website

Re: Accessing the author global variable via PHP

Sencer, sorry, I have only a smattering of PHP knowledge, so I’m not really sure what your asking me to look for?

Offline

#7 2006-07-20 12:25:13

Sencer
Archived Developer
From: cgn, de
Registered: 2004-03-23
Posts: 1,803
Website

Re: Accessing the author global variable via PHP

1) There is the txp:author-tag you should use, if all you want is the author’s name.

2) $GLOBALS['thisarticle']['author']

You cannot output this variable, because it does not exist. The error message tells you so. Notice: Undefined index: author on line 1 means you are trying to access an element of the global array $thisarticle with an index (author) that is not defined.

If you look at the code, it shows how textpattern fetches the author’s name. To adapt your snippet you would have to use something like: get_author_name($GLOBALS['thisarticle'][‘authorid’]); – however why you want to use php for something that is a built-in function/tag of textpattern, I do not know…

Also be aware that hacking in random php code may introduce security vulnerabilities to your site.

Offline

#8 2006-07-20 12:31:00

jonathanclarke
Member
From: London, England
Registered: 2004-12-28
Posts: 46
Website

Re: Accessing the author global variable via PHP

I know, normally I would use the built-in TXP tags. But I need some custom PHP code to enable me to create dynamic channels for Google Adsense units.

So, I’m using PHP to handle the dynamic channel logic, I then just need to access author name from within a PHP block. I think I’m right in thinking that the only way is the GLOBAL vars, as escaping PHP back to TXP isn’t allowed.

Last edited by jonathanclarke (2006-07-20 12:33:46)

Offline

#9 2006-07-20 12:48:53

jonathanclarke
Member
From: London, England
Registered: 2004-12-28
Posts: 46
Website

Re: Accessing the author global variable via PHP

Yes, I’d rather not hack the code, I’d like my installation to be upgradable.

Offline

#10 2006-07-20 13:31:40

jonathanclarke
Member
From: London, England
Registered: 2004-12-28
Posts: 46
Website

Re: Accessing the author global variable via PHP

AH, authorid, gives me the username! Great, thanks! That should be enough.

Offline

#11 2006-07-20 14:34:44

Sencer
Archived Developer
From: cgn, de
Registered: 2004-03-23
Posts: 1,803
Website

Re: Accessing the author global variable via PHP

authorid gives you the ID of the author (the thing with which he logs in). If you want the name, you have to additionally use the function-call, as I mentioned.

Offline

#12 2006-07-20 14:39:00

jonathanclarke
Member
From: London, England
Registered: 2004-12-28
Posts: 46
Website

Re: Accessing the author global variable via PHP

ID is good enough, thanks, Sencer!

Offline

Board footer

Powered by FluxBB