简体   繁体   中英

Pass a PHP variable from snippet to snippet in WordPress using the plugin 'Insert PHP Code Snippet'

How do I pass a PHP variable from snippet to snippet using the plugin "Insert PHP Code Snippet by XYZ"?

A snippet can be used by putting this [xyz-ics snippet=”snippetname”] in a textbox.

Note: the snippets cannot be merged together because the variable will be named in the first snippet, and this variable will be used in the second snippet.

Snippet 1:

<?php
   $test = 0;
?>

Snippet 2:

<?php
   echo $test;
?>

It somehow does not find the $test created in the first snippet.

I found How can I pass parameter using the snippet? with some information, but I am not sure if I can pass a PHP variable, because they only show hardcoded variables.

When in the same snippet, it does work:

<?php
  $test = 0;
  echo $test;
?>

Output: 0

There are two ways to do it. If you are on the same server then you can use $GET and if you are using a different server then you should create cookies of value that use it in the other snippet.

Cookies will hold your value until you clear yourself by code or when the cookies time out. Please see the manual of PHP cookies here .

You should declare a global.

Snippet 1:

<?php
global $test;
$test=0;
?>

Snippet 2:

<?php
global $test;
echo $test;
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM