Php WordPress非常简单的地理定位
我需要做一些非常基本的地理定位为WordPress网站 比如:Php WordPress非常简单的地理定位,php,json,wordpress,geolocation,Php,Json,Wordpress,Geolocation,我需要做一些非常基本的地理定位为WordPress网站 比如: <?php if ( visitor is from usa ): ?> <?php get_footer('us'); ?> <?php else: ?> <?php get_footer('other-countries'); ?> <?php endif; ?> 但我仍然需要帮助来整理最终的完整代码。试试这个: <?php $ip = ( ! empty( $
<?php if ( visitor is from usa ): ?>
<?php get_footer('us'); ?>
<?php else: ?>
<?php get_footer('other-countries'); ?>
<?php endif; ?>
但我仍然需要帮助来整理最终的完整代码。试试这个:
<?php
$ip = ( ! empty( $_SERVER['REMOTE_ADDR'] ) ) ?
$_SERVER['REMOTE_ADDR'] : '';
$country_code = '';
if ( $ip ) {
// 'fields' are comma-separated list of fields to include in the response.
// No spaces between the fields. See http://ip-api.com/docs/api:returned_values#field_generator
$url = 'http://ip-api.com/json/' . $ip . '?fields=countryCode,status,message';
$res_body = wp_remote_retrieve_body( wp_safe_remote_get( $url ) );
$geo = json_decode( $res_body );
$country_code = isset( $geo->countryCode ) ? $geo->countryCode : '';
}
if ( 'US' === $country_code ) : ?>
DO THE THIS
<?php else : ?>
DO THE THAT
<?php endif; ?>
做这个
做那件事
附言:我刚学会我应该使用代码
是的,这是使用jQuery的一个例子
$.getJSON( '//ip-api.com/json?callback=?', function( data ) {
console.log( JSON.stringify( data, null, 2 ) );
});
<?php
$ip = ( ! empty( $_SERVER['REMOTE_ADDR'] ) ) ?
$_SERVER['REMOTE_ADDR'] : '';
$country_code = '';
if ( $ip ) {
// 'fields' are comma-separated list of fields to include in the response.
// No spaces between the fields. See http://ip-api.com/docs/api:returned_values#field_generator
$url = 'http://ip-api.com/json/' . $ip . '?fields=countryCode,status,message';
$res_body = wp_remote_retrieve_body( wp_safe_remote_get( $url ) );
$geo = json_decode( $res_body );
$country_code = isset( $geo->countryCode ) ? $geo->countryCode : '';
}
if ( 'US' === $country_code ) : ?>
DO THE THIS
<?php else : ?>
DO THE THAT
<?php endif; ?>