簡體   English   中英

在 WordPress 頁面中插入 PHP 代碼並發布

[英]Insert PHP code In WordPress Page and Post

我想知道使用 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'];

默認情況下,WordPress 不會在帖子/頁面內容中執行 PHP,除非它有短代碼。

執行此操作的最快和最簡單的方法是使用一個插件,該插件允許您運行嵌入在帖子內容中的 PHP。

還有另外兩種“快速簡便”的方法可以在沒有插件的情況下完成它:

  • 使它成為一個簡碼(把它放在functions.php並讓它與國家名稱相呼應),這很容易 - 請參閱此處: WP Codex 的簡碼 API

  • 將它放在模板文件中- 根據您的默認頁面模板為該頁面創建一個自定義模板,並將 PHP 添加到模板文件而不是帖子內容中: 自定義頁面模板

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

說明

在帖子或頁面中運行 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 

您可以為此創建單獨的頁面模板,也可以使用此插件http://wordpress.org/plugins/exec-php/

創建頁面模板是一個不錯的選擇。

當我試圖完成一些非常相似的事情時,我最終按照以下方式做了一些事情:

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
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM