Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2023-03-07 19:50:22

lindabb
Member
Registered: 2023-02-17
Posts: 132

use variables inside plugin

Hello,

How can I call or use txp variables from plugin ? like site_url

Thank you

Offline

#2 2023-03-07 20:10:00

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

Re: use variables inside plugin

A few options:

Option 1: not very future proof as we may gradually deprecate global variables usage:

global $variable;

$value = $variable['site_url'];
echo $value;

Note that if you’re nesting multiple <txp:php> blocks on a page, the blocks do not share scope so you need to import globals into each one.

Option 2: use the function directly. At present, most tags have an equivalent function in the global scope. Not very future proof and no guarantee that functions are loaded or available without importing them first as we gradually migrate some tags to classes:

$value = variable(array('name' => 'site_url'));
echo $value;

Option 3: use parse(). Much more compatible and greater longevity of code, plus it allows tag attributes to be used more easily:

$value = parse('<txp:variable name="site_url" />');
echo value;

Stick with option 3 if you can.


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

#3 2023-03-07 20:45:35

etc
Developer
Registered: 2010-11-11
Posts: 5,157
Website GitHub

Re: use variables inside plugin

If you mean processing txp tags, for example

<txp:if_article_author name="lindabb"><txp:author /></txp:if_article_author>

another valuable option is

$result = processTags('if_article_author', array('name' => 'lindabb'), '<txp:author />');
// or
$result = processTags('if_article_author', 'name="lindabb"', '<txp:author />');

Offline

#4 2023-03-07 20:48:50

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

Re: use variables inside plugin

Ooh, forgot about option 4. Good call, etc. That one’s ace.


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

#5 2023-03-08 12:55:40

lindabb
Member
Registered: 2023-02-17
Posts: 132

Re: use variables inside plugin

Thank you both replies,
Sorry to say, none of those worked, returns empty , I’m using 4.8.9 dev version with PHP 8.1
Maybe my windows 10 computer configuration issue.
However, I was able to pass it as parameter and worked.

Offline

#6 2023-03-08 13:06:20

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

Re: use variables inside plugin

Very strange. Please show us your code and any surrounding relevant tags and we’ll try and figure out why.


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

#7 2023-03-08 13:26:26

lindabb
Member
Registered: 2023-02-17
Posts: 132

Re: use variables inside plugin

Thank you for your fast reply,
I don’t have any special tags, in fact I wasn’t able to use any of them, here is the code as the documentation to first plugin :

\Txp::get(‘\Textpattern\Tag\Registry’) ->register(‘abc_send_link’);

function abc_send_link($atts, $thing = null)
{

extract(lAtts(array( ‘siteurl’ => ‘’, // added later ‘filename’ => ‘’, ‘text’ => ‘Here is the download link for Spring 2023 Events And Happenings.’, ), $atts)); // $siteurl= parse(‘<txp:variable name=“site_url” />’); //echo $siteurl; didn’t show anything, so I added siteurl to pass it $thelink= $siteurl . ‘ /public/download/’ . $filename ; $msg = $text .’<br>’. $thelink; // send email mail(“someone@example.com”,“My subject”,$msg); return ; }

Thank you

Offline

#8 2023-03-08 14:29:57

etc
Developer
Registered: 2010-11-11
Posts: 5,157
Website GitHub

Re: use variables inside plugin

Are you sure you have set <txp:variable name="site_url" /> before calling your tag? Or did you mean <txp:site_url />?

Offline

#9 2023-03-08 14:51:16

lindabb
Member
Registered: 2023-02-17
Posts: 132

Re: use variables inside plugin

Thank you,
all I need to access site_url which is <txp:site_url /> on my plugin
sorry if my question wasn’t correct ,
do I need set <txp:variable name=“site_url” /> somewhere , like before call plugin ?

Thank you

Offline

#10 2023-03-08 15:07:51

etc
Developer
Registered: 2010-11-11
Posts: 5,157
Website GitHub

Re: use variables inside plugin

lindabb wrote #334971:

Thank you,
all I need to access site_url which is <txp:site_url /> on my plugin
sorry if my question wasn’t correct ,
do I need set <txp:variable name=“site_url” /> somewhere , like before call plugin ?

Why would you set it at all, if you can use <txp:site_url /> directly?

...
$siteurl = parse('<txp:site_url />');
...

<txp:variable /> serves another purpose and does not automatically store site parameters or other tags output. The site URL is stored in hu constant, so you don’t even need to parse anything:

...
$siteurl = hu;
...

Offline

#11 2023-03-08 16:10:38

lindabb
Member
Registered: 2023-02-17
Posts: 132

Re: use variables inside plugin

I apologies for the inconvenience and wasting your time, my mistake!
$siteurl = parse(‘<txp:site_url />’); worked,

I didn’t know the differences between variable and tag

Thank you all

Offline

#12 2023-03-08 18:11:20

etc
Developer
Registered: 2010-11-11
Posts: 5,157
Website GitHub

Re: use variables inside plugin

No worries and congrats for your first txp plugin.

Offline

Board footer

Powered by FluxBB