【问题标题】:Insert PHP code In WordPress Page and Post在 WordPress 页面中插入 PHP 代码并发布
【发布时间】:2013-09-24 14:59:12
【问题描述】:

我想知道 visitor 使用 PHP 的国家并将其显示在 WordPress 页面上。但是当我将 PHP 代码添加到 WordPress 页面或发布时,它会给我一个错误。

我们如何在 WordPress 页面和帖子上添加 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'];

【问题讨论】:

  • 这会给你带来什么错误?

标签: php wordpress


【解决方案1】:

说明

在帖子或页面中运行 PHP 代码有 3 个步骤。

  1. functions.php 文件中(在您的主题中)添加新功能

  2. functions.php 文件(在您的主题中)注册调用您的函数的新短代码:

add_shortcode( 'SHORCODE_NAME', 'FUNCTION_NAME' );
  1. 使用新的简码

示例 #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 

【讨论】:

    【解决方案2】:

    当我试图完成非常相似的事情时,我最终做了以下几件事:

    wp-content/themes/resources/functions.php

    add_action('init', 'my_php_function');
    function my_php_function() {
        if (stripos($_SERVER['REQUEST_URI'], 'page-with-custom-php') !== false) {
            // add desired php code here
        }
    }
    

    【讨论】:

      【解决方案3】:

      您不能在 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

      【讨论】:

        【解决方案4】:

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

        最快和最简单的方法是使用一个插件来运行嵌入在帖子内容中的 PHP。

        还有另外两种“快速简单”的方法可以在没有插件的情况下完成它:

        • 将其设为简码(将其放入 functions.php 并让它与国家名称相呼应),这非常简单 - 请参见此处:Shortcode API at WP Codex

        • 将其放入模板文件中 - 根据您的默认页面模板为该页面制作自定义模板,并将 PHP 添加到模板文件而不是帖子内容中:Custom Page Templates

        【讨论】:

        • 我正在使用自定义主题,我从管理后端创建了一个页面,并且我只想为此页面使用不同的标题结构(页面顶部)。我不能在这里使用模板文件。我只想使用后端页面。我在那个设置一个变量中放了一个短代码并在 header.php 中检查了该值是否得到?因此,根据该值,我将显示此页面所需的 header.php 部分。但是这个变量的值是空白的。请告诉我解决方案
        猜你喜欢
        • 2011-04-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-03-18
        • 2019-03-23
        • 2020-05-26
        • 1970-01-01
        • 2016-02-03
        相关资源
        最近更新 更多