我想知道使用PHP的访客国家并将其显示在WordPress页面中.但是当我在WordPress页面或Post中添加PHP代码时,它会给我错误.我们如何在Wordpress Page和Post中添加PHP代码.
  <?PHP
   try{
        function visitor_country()
            {
                $client  = @$_SERVER['HTTP_CLIENT_IP'];
                $forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
                $remote  = $_SERVER['REMOTE_ADDR'];
                $result  = "Unknown";
                if(filter_var($client, FILTER_VALIDATE_IP))
                {
                    $ip = $client;
                }
                elseif(filter_var($forward, FILTER_VALIDATE_IP))
                {
                    $ip = $forward;
                }
                else
                {
                    $ip = $remote;
                }
                $ip_data = @json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=".$ip));
                if($ip_data && $ip_data->geoplugin_countryName != null)
                {
                    $result = array('ip'=>$ip,
                                    'continentCode'=>$ip_data->geoplugin_continentCode,
                                    'countryCode'=>$ip_data->geoplugin_countryCode,
                                    'countryName'=>$ip_data->geoplugin_countryName,
                                    );
                }
                return $result;
            }
           $visitor_details= visitor_country(); // Output Country name [Ex: United States]
           $country=$visitor_details['countryName'];
Enn*_*nui 57
默认情况下,WordPress不会在帖子/页面内容中执行PHP,除非它有短代码.
最快捷,最简单的方法是使用一个插件,允许您运行嵌入在帖子内容中的PHP.
没有插件,还有另外两种"快速简便"的方法来实现它:
将它设为短代码(将其放入functions.php并使其回显国家名称)非常简单 - 请参阅此处:WP Codex的短代码API
将其放在模板文件中 - 根据您的默认页面模板为该页面创建自定义模板,并将PHP添加到模板文件而不是帖子内容中:自定义页面模板
Din*_*esh 13
您不能在WordPress后端页面编辑器中使用PHP.也许你可以使用插件,但不是开箱即用.
最简单的解决方案是创建一个短代码.然后你可以使用这样的东西
function input_func( $atts ) {
    extract( shortcode_atts( array(
        'type' => 'text',
        'name' => '',
    ), $atts ) );
    return '<input name="' . $name . '" id="' . $name . '" value="' . (isset($_GET\['from'\]) && $_GET\['from'\] ? $_GET\['from'\] : '') . '" type="' . $type . '" />';
}
add_shortcode( 'input', 'input_func' );
请参阅Shortcode_API.
Bok*_*ken 13
说明:
在帖子或页面中运行 PHP 代码有 3 个步骤。
在functions.php文件中(在您的主题中)添加新功能
在functions.php文件中(在您的主题中)注册调用您的函数的新短代码:
add_shortcode( 'SHORCODE_NAME', 'FUNCTION_NAME' );
示例#1:只显示文本。
在函数中:
function simple_function_1() {
    return "Hello World!";
}
add_shortcode( 'own_shortcode1', 'simple_function_1' );
在帖子/页面中:
[own_shortcode1]
影响:
Hello World!
示例 #2:使用 for 循环。
在函数中:
function simple_function_2() {
    $output = "";
    
    for ($number = 1; $number < 10; $number++) {    
        // Append numbers to the string
        $output .= "$number<br>";
    } 
    
    return "$output";
}
add_shortcode( 'own_shortcode2', 'simple_function_2' );
在帖子/页面中:
[own_shortcode2]
影响:
1
2
3
4
5
6
7
8
9
示例 #3:使用带参数的简码
在函数中:
function simple_function_3($name) {
    return "Hello $name";
}
add_shortcode( 'own_shortcode3', 'simple_function_3' );
在帖子/页面中:
[own_shortcode3 name="John"]
影响:
Hello John
示例 #3 - 不传递参数
在帖子/页面中:
[own_shortcode3]
影响:
Hello 
| 归档时间: | 
 | 
| 查看次数: | 133050 次 | 
| 最近记录: |