Php 访问钩子中的变量?

Php 访问钩子中的变量?,php,wordpress,Php,Wordpress,我正在制作一个WP插件,在我的插件中我有一些功能 第一个是查找用户的位置,另一个是运行一些逻辑,这取决于位置的输出,但是此函数将其挂接到_post,如下所示: function find_location() { ... $countrycode = $obj->country_code; ... } function everypost_func($obj) { ... echo $countrycode; ... } add_action('the_post','everypost

我正在制作一个WP插件,在我的插件中我有一些功能

第一个是查找用户的位置,另一个是运行一些逻辑,这取决于位置的输出,但是此函数将其挂接到_post,如下所示:

function find_location() {
...
$countrycode = $obj->country_code;
...
}

function everypost_func($obj) {
...
echo $countrycode;
...
} 
add_action('the_post','everypost_func');
我尝试过使用全局变量,但这些似乎不起作用。有人能解释一下情况吗?
我面临的问题是如何访问$countrycode变量,在find_location函数之外

您是否考虑过像这样将变量传递给函数:

function find_location($obj) {
  //...
  $countrycode = $obj->country_code;
  //...
  return $countrycode;
}

function everypost_func($obj) {
  //...
  $countrycode = find_location($obj);
  echo $countrycode;
  //...
} 
add_action('the_post','everypost_func');
如果$obj中有更多的值,您需要访问它,您可以这样做

function find_location() {
...
$countrycode = $obj->country_code;
...
return $obj;
}

function everypost_func() {
$object = find_location();
...
$countrycode = $object->country_code;
echo $countrycode;
...
} 
add_action('the_post','everypost_func');

有什么问题?
function find_location() {
...
$countrycode = $obj->country_code;
...
return $obj;
}

function everypost_func() {
$object = find_location();
...
$countrycode = $object->country_code;
echo $countrycode;
...
} 
add_action('the_post','everypost_func');