0

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');

?>`
  • The lifetime of a variable is the request it was set within. If you're trying to persist it beyond one request, you need to store it somewhere, like in an option. – Milo Feb 17 '15 at 23:21
  • you can add your working code as an answer so that it may help others in the future. – Milo Feb 18 '15 at 4:53
  • Just to add, your answer will not only help future users, but you can gain yourself some reputation which is useful in unlocking more features which gives you more usage of the site :-) – Pieter Goosen Feb 18 '15 at 6:02
0

Here is the solution I came up with using options. Thanks for the advice this worked well for my use case.

<?php
// This function sends the http request that purges my cdn cache.
function hw_cdn_purge() {

// My http request code goes here.
// After the request the purgeCdnResult variable is set to the status code of the http request.  I will demo that manually here.
$purgeCdnResult = 200;

// I then pass theis variable to an option in the database to keep it beyond this function.    
update_option( 'purge_response_status', $purgeCdnResult;
return;
}

// This next function checks the stored status code from the option and changes the displayed message.
function update_post_published_message( $messages ) {

// First we retrieve our variable from our stored option or set it to 1 if it is not found.
$purgeCdnResult = get_option( 'purge_response_status', 1 );

// Now we check our result and change the messages appropriately.
if ($purgeCdnResult == '200') {
$messages['post'][6] = sprintf(__('Post Published and CDN has been Purged.  Status Code '.$purgeCdnResult));
$messages['post'][1] = sprintf(__('Post Updated and CDN has been Purged.  Status Code '.$purgeCdnResult));
} else {
$messages['post'][6] = sprintf(__('Post Published - Error Purging Cache.   Status Code '.$purgeCdnResult));
$messages['post'][1] = sprintf(__('Post Updated - Error Purging Cache.  Status Code '.$purgeCdnResult));
}

// Finally we update the option to a non-successful value so that we don't get false positive results in the future.
update_option( 'purge_response_status', 1);
return $messages;
}

// Now we set the first function up to execute when the publish_post action is called
add_action( 'publish_post', 'hw_cdn_purge' );

// now we hookup the message changer function
add_filter( 'post_updated_messages', 'update_post_published_message');

?>`

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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