首页 > 解决方案 > 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 &#8222;> <a href="https://XXX.wufoo.com/forms/YYYYY/def/field414=ABCDE &#8222;>">Link to survey</a>

所以而不是field414=ABCDE">它说field414=ABCDE &#8222;>

我对 PHP 很陌生,并认为函数中可能存在问题,但无法弄清楚。

有人在某处看到错误吗?

谢谢,帕特里克

标签: phpwordpress

解决方案


正确设置 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;
}

推荐阅读