从函数内部编辑全局变量

时间:2015-02-17 作者:Tim Snider

我正在开发我的第一个插件,里面有两个独立的函数来完成两个不同的任务。每个函数都添加到不同的挂钩中,一个使用动作,另一个使用过滤器。第二个函数(过滤器)需要能够访问与第一个函数相同的变量,以便我可以根据第一个函数的状态做出决策。我正试图使用Globals($Globals[\'varname\'))来实现这一点。我的问题是,即使我可以验证(通过我正在进行的http调用)第一个函数是否正在运行。它没有设置全局变量。因此,第二个函数总是以相同的方式执行。我一直在浏览文档和论坛,但似乎找不到答案。这里有人能给我指出一个可靠的方向吗?下面是一些示例代码,但我已经删除了http请求。

`

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

?>`

1 个回复
SO网友:Tim Snider

下面是我使用选项提出的解决方案。谢谢你的建议,这对我的用例很有效。

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

?>`

结束

相关推荐

Php显示文章循环,而不是页面内容

我的首页显示最新帖子,使用index.php.我的page.php 包含wp_header 在顶部wp_footer 以下,以及介于两者之间的内容:while (have_posts()) : the_post(); get_template_part( \'content\', \'page\' ); endwhile; 我的content-page.php 应该打印出来the_content() 以及the_title() 但它返回所有帖子的内容和标题。我错过了什么?