在WordPress页面中插入PHP代码并发布 [英] Insert PHP code In WordPress Page and Post

查看:400
本文介绍了在WordPress页面中插入PHP代码并发布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道使用PHP的访问者国家/地区并将其显示在WordPress页面中,但是当我在WordPress页面中添加PHP代码或将其发布时给我错误. 我们如何在WordPress Page and Post中添加PHP代码.

I want to know Visitor country using PHP and display it in to a WordPress Page.But when I add PHP code in WordPress page or Post it give me error. How can we add PHP code in WordPress Page and Post.

  <?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'];

推荐答案

默认情况下,WordPress不会在帖子/页面内容中执行PHP,除非它具有简码.

WordPress does not execute PHP in post/page content by default unless it has a shortcode.

最快,最简单的方法是使用一个插件,该插件可让您运行嵌入在帖子内容中的PHP.

The quickest and easiest way to do this is to use a plugin that allows you to run PHP embedded in post content.

还有另外两种快速简便"的方法无需插件即可完成:

There are two other "quick and easy" ways to accomplish it without a plugin:

  • 为其输入简码(将其放入functions.php并与国家/地区名称相呼应),这非常容易-参见此处:

  • Make it a shortcode (put it in functions.php and have it echo the country name) which is very easy - see here: Shortcode API at WP Codex

将其放入模板文件-根据您的默认页面模板为该页面创建自定义模板,然后将PHP添加到模板文件而不是帖子内容中:自定义页面模板

Put it in a template file - make a custom template for that page based on your default page template and add the PHP into the template file rather than the post content: Custom Page Templates

这篇关于在WordPress页面中插入PHP代码并发布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆