I'm working on my first plugin and I have two separate functions inside to accomplish two different task. Each function is added to a different hook, one using actions and the other using filters. The second function(the filter) needs to be able to access the same Variable as the first so that I can make a decision based on the status of the first function. I'm trying to achieve this using Globals ($GLOBALS['varname'}]). My problem is that even though I can verify (through an http call I'm making )that the first function is running . It is not setting the global variable. So the second function is always executing the same way. I've been scouring through documentation and forums but can't seem to find an answer. Can anyone here point me in a solid direction? Here is some sample code but I've removed my http request.
`
<?php
// DEFINE MY PURGE CDN VARIABLE AND SET ITS INITIAL VALUE
$purgeCdnResult = 7;
// THIS FUNCTION SEND THE HTTP REQUEST THAT PURGES THE CDN CACHE
function hw_cdn_purge() {
// SET MY PURGE CDN GLOBAL VARIABLE TO 1
$GLOBALS['purgeCdnResult'] = 1;
// MY HTTP REQUEST CODE GOES HERE
return;
}
// THIS FUNCTION CHECKS THE STATS OF THE GLOBAL PURGE CDN VARIABLE AND CHANGES THE MESSAGE APPROPRIATELY
function update_post_published_message( $messages ) {
if ($GLOBALS['purgeCdnResult'] == 1) {
$messages['post'][6] = sprintf(__('Post Published and CDN has been Purged.'));
} else {
$messages['post'][6] = sprintf(__('Post Published :-) - '.$GLOBALS['purgeCdnResult'].' was the statuscode.'));
}
return $messages;
}
// Now we set that function up to execute when the publish_post action is called
add_action( 'publish_post', 'hw_cdn_purge' );
// now we hookup the message changer
add_filter( 'post_updated_messages', 'update_post_published_message');
?>`