Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
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
Re: Accessing the author global variable via PHP
PS. I do have PHP enabled in the advanced settings for Textpattern.
Offline
Re: Accessing the author global variable via PHP
Look at what function author($atts) does (in publish/taghandlers.php).
Offline
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
Re: Accessing the author global variable via PHP
Do I need to supply author() with some more information?
Offline
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
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
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
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
Re: Accessing the author global variable via PHP
AH, authorid, gives me the username! Great, thanks! That should be enough.
Offline
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
Re: Accessing the author global variable via PHP
ID is good enough, thanks, Sencer!
Offline