• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

BUG Not able to pass PHP variable

Messages
3
Likes
0
Points
1
#1
Hi there!

I'm trying to pass a PHP variable onto one of my custom edited plugins.

I'll try to describe below:

On one of my custom edit php files of "Eventon - Action User" WP plugin, I've a dropdown which I want to control the dropdown options via a PHP variable, like:

$variable = [
"opt1" => "/opt1",
"opt2" => "/opt2",
"opt3" => "/opt3",
];
... more code!

I've manged to do this directly on the .php file. But what I want to achieve is to just replace the variable with a shortcode, where I can have the variable inside the shortcode, so I can manage the dropdown options through Woody Snippets, and don't have to necessarily edit the php file.

This is what I'm doing and it's not working:

echo do_shortcode('[wbcr_php_snippet id="6296"]');
... more code!

and... in the Woody Snippet "6296" I have:
$variable = [
"opt1" => "/opt1",
"opt2" => "/opt2",
"opt3" => "/opt3",
];


Am I doing something wrong?

Cheers Alex!
 
Messages
3
Likes
0
Points
1
#2
UPDATE

I'm able to pass a simple: echo "variable test";
But I'm not able to pass the variable array... or it does pass the array, but for some reason it's not being loaded by the php onto the dropdown...
 
Messages
3
Likes
0
Points
1
#3
UPDATE

I'm sorry for the post. I've managed to make it work. EVERYTHING must be inside the snippet in order to work. Mainly the "echo '<select..." so that was what I was missing:

Hopefully this post will help someone, otherwise, just delete it.

Thanks anyway. You have a great plugin.

Cheers,
Alex
 

Temyk

Developer & Support
Staff member
Messages
744
Likes
32
Points
28
#4
Hello.

You can also use the global keyword for example like this in snippet:
PHP:
global $variable = [
    "opt1" => "/opt1",
    "opt2" => "/opt2",
    "opt3" => "/opt3",
];
and like this in plugin:
PHP:
global $variable;
echo $variable;
But make sure that the snippet code is executed before the plugin code.

But since this variable becomes global, you need to give it a unique name so that there are no conflicts with other plugins or themes