I have a custom plugin file that contains a function to generate a string upon the firing of a 3rd-party plugin hook. I want to echo this string in a custom template file — how do I give the template file access to this generated string? Most responses to similar questions advocate the use of global variables, but they don't work in my case.

plugin file looks like:

global $appeal;

function makeAppeal($op, $objectName, $objectId) {
    // long block that generates a string and assigns it to $generatedAppeal

    return $generatedAppeal;

function appeal_generator_set_global_var($op, $objectName, $objectId, &$objectRef) {
    global $appeal;
    $appeal = makeAppeal($op, $objectName, $objectId);

add_action( "civicrm_postCommit", "appeal_generator_set_global_var", 10, 4 );

custom template:

global $appeal
header('Content-Type: application/json; charset=utf-8');
echo json_encode($appeal);

When I visit a page that has this template, I see null instead of the string inside global $appeal. Is this a scope issue? Is it possible to use a global variable when the value of $appeal will keep changing as per the firing of the 3rd-party plugin hook? If not, then what other ways are there to share this plugin variable with the template?

  • This isn't how global variables work. You need to save the variable to the database and retrieve it in the template. Jun 30, 2023 at 14:47
  • Would a session work for you? <?php session_start(); $_SESSION['appeal'] = $appeal and then in the other file: <?php session_start(); $appeal = $_SESSION['appeal'];
    – breadwild
    Jun 30, 2023 at 15:03
  • If you have DB calls = not a typical WordPress template (it's not using any of the calls like have_posts or the_post), e.g., a file containing only database and ajax for processing, you need to refire the DBI with something like require_once($_SERVER['DOCUMENT_ROOT'].'/wp-load.php');
    – breadwild
    Jun 30, 2023 at 16:25
  • Yes @JacobPeattie you are right, the variable isn't saved between files. I was able to solve the issue by saving the variable to the DB of the 3rd-party plugin I was using and access it that way. Jul 1, 2023 at 17:53

1 Answer 1


Your code has the correct syntax, but for globals to work they have to be run in the same PHP execution (i.e. the same page load), and you'll need to set that global before you read it. Globals are completely separate to each page load.

You either have a case where the code in the template is running before the plugin has run to set the global, or you're trying to access the variable in a completely different page load to when you set it, so it won't be available. If your 'template' file isn't part of Wordpress at all, Wordpress will never run to set the global.

So you have a few options:

  • If your template isn't part of Wordpress, load Wordpress from your template so that the plugin runs and sets the global. See @breadwild's comment.
  • Put the makeAppeal function in a library somewhere so that you can call it from both inside and outside of Wordpress to get the appeal string value from the template. If you don't load Wordpress, this relies on that function not calling Wordpress functionality.

I'm assuming that the $appeal value changes depending on a particular case or event or something the user is looking at, so you could also look at persisting that value in the WP database. For example, if the appeal value is associated with a WP custom post type, you can use update_post_meta and get_post_meta to persist those values in the database. Again, that would mean you'd need to load Wordpress from your template in order to have access to the get_post_meta function, and ensure that it's set before you try to access it.

  • Yes, it was an issue of the variable being in two separate pages and as such not being available. I found a fairly simple way to work around the problem. I initially wanted to share the variable between the files to echo it from the template and ultimately access it with ajax in another plugin file, but I was able to save the variable to the database of the 3rd-party plugin I was using, so I could access it that way. Jul 1, 2023 at 17:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.