我创建了一个插件,可以获取用户的国家位置详细信息,包括城市,国家和邮政编码。
在插件中有两个功能1可以获取国家/地区,然后另一个功能可以获取用户国家/地区,城市和邮政编码,并为所有用户详细信息输出数组。在模板中,我正在尝试var_dump要测试的数据,并且在我的实时网站上,它可以正常运行:
string(25177) "{"700":["700","--",52.660348,-3.146407,"members"],"1517":["1517","--",53.8007554,-1.5490774,"members"],"767":["767","--",55.864237,-4.251806,"members"],"4267":["4267","--",54.978252,-1.61778,"members"],"10291":["10291","--",27.6648274,-81.5157535,"members"],"178":["178","--",52.9225301,-1.4746186,"members"],"1789":["1789","--",52.9225301,-1.4746186,"members"],"5029":["5029","--",55.953252,-3.188267,"members"],
等等
这是整个功能:
function shiftms_userloc_get_user_location( $userid = false ) {
if ( !$userid ) {
if ( !bp_displayed_user_id() ) {
return false;
} else {
$userid = bp_displayed_user_id();
}
}
$location = '';
$country = bp_get_profile_field_data( 'field=Country&user_id=' . $userid );
$city = bp_get_profile_field_data( 'field=City&user_id=' . $userid );
$zip = bp_get_profile_field_data( 'field=Postcode/Zip code&user_id=' . $userid );
if( $city ) {
$location .= $city;
if( $zip ) {
$location .= ' ' . $zip;
}
} else {
if( $zip ) {
$location .= $zip;
}
}
if ( $country ) {
if( $location ) {
$location .= ', ' . $country;
} else {
$location .= $country;
}
}
return $location;
}
function shiftms_userloc_get_locations( $format = 'json', $ajax = true ) {
$locations = array();
//if ( false === ( $locations = get_transient( 'shiftms_userlocations' ) ) ) {
$args = array( 'fields' => 'ID' );
$users = new WP_User_Query( $args );
if ( ! empty( $users->results ) ) {
foreach ( $users->results as $userid ) {
$location = shiftms_userloc_get_user_location( $userid );
if( $location ) {
$geo = get_user_meta( $userid, 'shiftms-geo', true );
if ( $geo ) {
$latlong = explode('/', $geo );
if ( is_array($latlong) && ( 2 == count( $latlong ) ) ) {
if ( 0 != $latlong[0] && 0 != $latlong[1] )
$locations[ $userid ] = array( $userid, $location, (float)$latlong[0], (float)$latlong[1], 'members' );
}
}
}
}
}
//}
$return = json_encode( $locations );
if ( $ajax ) {
echo $return;
die();
} else {
return $return;
}
}
在本地网站上我得到了这个:
string(2) "[]"
创建数组的函数是:
$locations[ $userid ] = array( $userid, $location, (float)$latlong[0], (float)$latlong[1], 'members' );
我认为这是我的php版本的问题,但我不确定。所有数据都在那里,因为它是相同的数据库等,当我输出当前用户国家,城市和邮政编码时,它的工作原理。它可能是我想要获得的数组类型。