在 WordPress 页面中插入 PHP 代码并发布
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18896146/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Insert PHP code In WordPress Page and Post
提问by Prince
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 的访问者国家并将其显示到 WordPress 页面中。但是当我在 WordPress 页面中添加 PHP 代码或发布时,它给了我错误。我们如何在 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'];
回答by Ennui
WordPress does not execute PHP in post/page content by default unless it has a shortcode.
默认情况下,WordPress 不会在帖子/页面内容中执行 PHP,除非它有短代码。
The quickest and easiest way to do this is to use a plugin that allows you to run PHP embedded in post content.
执行此操作的最快和最简单的方法是使用一个插件,该插件允许您运行嵌入在帖子内容中的 PHP。
There are two other "quick and easy" ways to accomplish it without a plugin:
还有另外两种“快速简便”的方法可以在没有插件的情况下完成它:
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 CodexPut 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
使它成为一个简码(把它放进去
functions.php
并让它与国家名称相呼应),这很容易 - 请参阅此处:WP Codex 的 Shortcode API将它放在模板文件中- 根据您的默认页面模板为该页面创建一个自定义模板,并将 PHP 添加到模板文件而不是帖子内容中:自定义页面模板
回答by Dinesh
You can't use PHP in the WordPress back-end Page editor. Maybe with a plugin you can, but not out of the box.
您不能在 WordPress 后端页面编辑器中使用 PHP。也许使用插件可以,但不能开箱即用。
The easiest solution for this is creating a shortcode. Then you can use something like this
最简单的解决方案是创建一个短代码。然后你可以使用这样的东西
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' );
See the Shortcode_API.
请参阅Shortcode_API。
回答by John Langford
When I was trying to accomplish something very similar, I ended up doing something along these lines:
当我试图完成一些非常相似的事情时,我最终按照以下方式做了一些事情:
wp-content/themes/resources/functions.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
}
}
回答by Boken
Description
描述
There are 3 steps to run PHP code inside post or page.
在帖子或页面中运行 PHP 代码有 3 个步骤。
1) In functions.php
file (in your theme) add new function
1)在functions.php
文件中(在您的主题中)添加新功能
2) In functions.php
file (in your theme) register new shortcode which call your function:
2)在functions.php
文件中(在您的主题中)注册调用您的函数的新短代码:
add_shortcode( 'SHORCODE_NAME', 'FUNCTION_NAME' );
3) use your new shortcode
3) 使用你的新简码
Example #1
示例#1
Just display text.
只显示文字。
In functions:
在函数中:
function simple_function_1() {
return "Hello World!";
}
add_shortcode( 'own_shortcode1', 'simple_function_1' );
In post/page:
在帖子/页面中:
[own_shortcode1]
Effect:
影响:
Hello World!
Example #2
示例#2
Use for loop.
使用 for 循环。
In functions:
在函数中:
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' );
In post/page:
在帖子/页面中:
[own_shortcode2]
Effect:
影响:
1
2
3
4
5
6
7
8
9
Example #3
示例 #3
Use shortcode with arguments
使用带参数的简码
In functions:
在函数中:
function simple_function_3($name) {
return "Hello $name";
}
add_shortcode( 'own_shortcode3', 'simple_function_3' );
In post/page:
在帖子/页面中:
[own_shortcode3 name="John"]
Effect:
影响:
Hello John
Example #3 - without passing arguemtns
示例 #3 - 不传递参数
In post/page:
在帖子/页面中:
[own_shortcode3]
Effect:
影响:
Hello