php - Wordpress:在没有插件的情况下在内容区域正确添加 PHP 代码
问题描述
我想在 Wordpress 页面的内容区域中添加一些 PHP 代码(通过 get_query_var 读取 URL 变量)。为此,我在 functions.php 中使用以下函数
function php_execute($html){
if(strpos($html,"<"."?php")!==false){ ob_start(); eval("?".">".$html);
$html=ob_get_contents();
ob_end_clean();
}
return $html;
}
add_filter('the_content','php_execute',100);
有了这个,我可以读取我的 URL 的 URL/thank-you/?order_id=ABCDE
变量<?php echo get_query_var( 'order_id' );?>
。
现在我想将此订单 ID 添加到 URL(以便在调查中预填充字段)并尝试将其添加到相应的 URL,如下所示:
<iframe src="https://XXX.wufoo.com/embed/YYYYY/def/field414=<?php echo get_query_var( 'order_id' );?>"> <a href="https://XXX.wufoo.com/forms/YYYYY/def/field414=<?php echo get_query_var( 'order_id' );?>">Link to survey</a>
不幸的是,生成的源代码如下所示:
<iframe src="https://XXX.wufoo.com/embed/YYYYY/def/field414=ABCDE „> <a href="https://XXX.wufoo.com/forms/YYYYY/def/field414=ABCDE „>">Link to survey</a>
所以而不是field414=ABCDE">
它说field414=ABCDE „>
我对 PHP 很陌生,并认为函数中可能存在问题,但无法弄清楚。
有人在某处看到错误吗?
谢谢,帕特里克
解决方案
正确设置 iframe 标记。然后将您的 php_execute 函数更改为此。
add_filter( 'the_content', 'php_execute' );
function php_execute( $content )
{
$orderId = isset($_GET['order_id']) ? trim($_GET['order_id']) : false;
if ($orderId) {
$content .= '<iframe src="https://XXX.wufoo.com/embed/YYYYY/def/field414=' . $orderId . '">';
$content .= '<a href="https://XXX.wufoo.com/forms/YYYYY/def/field414=' . $orderId . '">Link to survey</a>';
}
return $content;
}
推荐阅读
- python - 打印总和等于目标总和的数组的所有子集
- nuget - 获取 Nuget 包的发布日期
- list - 如何更改 kivymd 中其他 kv 字符串的属性?
- python - 仅在python中具有空值的列的数据类型
- firebase - Flutter Web & Firebase - 从 JSON API 接收的存储/日志信息
- java - 在 Spring Boot 项目中为特定用户启用 Swagger UI
- rust - 是否可以创建一个 Box<(T, [U])>?
- c - 将简单的 C 程序手动转换为汇编程序
- loops - 如何在带有变量的循环中运行 awk
- reactjs - 在 Gatsby 的所有页面中扩展背景视频和导航栏