下面是我使用选项提出的解决方案。谢谢你的建议,这对我的用例很有效。
<?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\');
?>`