使用 htmlspecialchars 动态标题作为短代码的参数

Using htmlspecialchars dynamic title as a parameter for a shortcode

我正在使用 post 标题作为短代码参数,但当页面标题中包含 & 时它不能完全正常工作。 'Communications & Media Coordinator'.

我正在尝试执行 htmlspecialchars,但它仍然只输出 'Communications'。

$jobtitlestr = htmlspecialchars($jobtitle);

echo do_shortcode('[gravityform id="1" field_values="job_title=' . $jobtitlestr . '&job_location=' . $locationtitle . '&list_count='" ajax="true" tabindex="1"]');

谢谢

假设此短代码是 GET 查询的一部分,“&”用作术语之间的分隔符并解释为什么您的标题被截断。

在 PHP 中,htmlspecialchars 将某些字符转换为其等效的 HTML 实体。 '&' 字符将变为 '&'如果您将它用作 URI 的一部分,那么仍然会出现问题。

我怀疑您需要的函数是 urlencode。您也必须通过另一端的 urldecode 运行 它。

https://www.php.net/manual/en/function.urlencode.php