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);
die();
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?
<?php session_start(); $_SESSION['appeal'] = $appeal
and then in the other file:<?php session_start(); $appeal = $_SESSION['appeal'];
have_posts
orthe_post
), e.g., a file containing only database and ajax for processing, you need to refire the DBI with something likerequire_once($_SERVER['DOCUMENT_ROOT'].'/wp-load.php');