简码 API 参数像国会分支中的逻辑一样被拒绝
Shortcode API parameters rejected like logic in the Congressional Branch
我已经看过这个问题的次数多得我都数不过来了,但我终究无法弄清楚我做错了什么。我过去写过很多次短代码函数,而且次数多得我数不过来。无论出于何种原因,这个都拒绝正常工作。经过一个小时的反复尝试,我没有找到成功。请帮助。
默认值出现在html就好了,但不会反映短代码中设置的参数。
- 我试过将 ID 视为字符串和整数。
- 也许 wordpress 不喜欢我尝试使用 attr
ID
... 已尝试 listID
,但仍然失败。
- 我尝试过使用
extract
与将 shortcode_attr()
的结果保存到变量中。
- 以为可能是插件或 post_type 阻止了我获胜,但是在从 Wordpress.org 中获取示例短代码 API 代码并将其插入 functions.php 和将他们的简码放在我的 post 中,它起作用了。 我要疯了
- 我已经清除了对象缓存,为了对此进行调试,我更改了函数中的默认值,更改后会立即显示 client-side。
- 可能简码 API 不喜欢我构建字符串。使用
ob_start()
和 ob_get_clean()
将其保存到缓冲区中(例如,洗个澡?)... 不开心
- 不,
var_dump
和 print_r
对我没有任何帮助,没有输出。即使我尝试 var_dump(array('foo' => 'bar'))
或 print_r(array('foo' => 'bar'))
我的理智现在正在接受超出合理措施的测试,我正在幻想对 Matt Mullenweg 的智能手机进行 DDOS 攻击
请治愈我的疯狂,并指出我明显忽略的微小细节这样我就可以继续我的一天。
简码
[subscription ID="1" referrer="overlay-promotion" buttonText="Send it to me" buttonBGHex="#000c49"]
函数
function subscription_form($atts){
//Yes, I tried extract to, and yes, I changed the variables in the html below to reflect this change.
$a = shortcode_atts( array(
"ID" => "1",
"referrer" => "not-what-you-think-this-should-be",
"buttonText" => "Subscribe",
"buttonBGHex" => "#78C138"
), $atts, "subscription" );
$html = '<form id="subscribe" name="email_signup" class="form-inline validate" action="http://www.example.com/subscribe" method="post">';
$html .= '<input type="hidden" name="referrer" value="'.$a["referrer"].'">';
$html .= '<input type="hidden" name="success_url" value="http://www.example.com/subscribe/success">';
$html .= '<input type="hidden" name="error_url" value="http://www.example.com/subscribe/error">';
$html .= '<input type="hidden" name="list_id" value="'.$a["ID"].'">';
$html .= '<input type="text" placeholder="Enter your email address" class="input-large" name="email">';
$html .= '<input type="submit" class="btn btn-primary" style="background-color:'.$a["buttonBGHex"].';" name="submit" value="'.$a["buttonText"].'">';
$html .= '</form>';
return $html;
}
add_shortcode( 'subscription', 'subscription_form' );
输出
<form id="subscribe" name="email_signup" class="form-inline validate" action="https://www.example.com/subscribe" method="post">
<input type="hidden" name="referrer" value="HP-overlay-2015">
<input type="hidden" name="success_url" value="https://www.example.com/subscribe/success">
<input type="hidden" name="error_url" value="https://www.example.com/subscribe/error">
<input type="hidden" name="list_id" value="1"><input type="text" placeholder="Enter your email address" class="input-large" name="email">
<input type="submit" class="btn btn-primary" style="background-color:#78C138;" name="submit" value="Subscribe">
</form>
根据 add_shortcode
的法典,短代码的属性名称被转换为小写。
Shortcode attribute names are always converted to lowercase before they are passed into the handler function. Values are untouched.
这意味着您需要使用小写版本访问数组键以获得正确的输出。
我已经看过这个问题的次数多得我都数不过来了,但我终究无法弄清楚我做错了什么。我过去写过很多次短代码函数,而且次数多得我数不过来。无论出于何种原因,这个都拒绝正常工作。经过一个小时的反复尝试,我没有找到成功。请帮助。
默认值出现在html就好了,但不会反映短代码中设置的参数。
- 我试过将 ID 视为字符串和整数。
- 也许 wordpress 不喜欢我尝试使用 attr
ID
... 已尝试listID
,但仍然失败。 - 我尝试过使用
extract
与将shortcode_attr()
的结果保存到变量中。 - 以为可能是插件或 post_type 阻止了我获胜,但是在从 Wordpress.org 中获取示例短代码 API 代码并将其插入 functions.php 和将他们的简码放在我的 post 中,它起作用了。 我要疯了
- 我已经清除了对象缓存,为了对此进行调试,我更改了函数中的默认值,更改后会立即显示 client-side。
- 可能简码 API 不喜欢我构建字符串。使用
ob_start()
和ob_get_clean()
将其保存到缓冲区中(例如,洗个澡?)... 不开心 - 不,
var_dump
和print_r
对我没有任何帮助,没有输出。即使我尝试var_dump(array('foo' => 'bar'))
或print_r(array('foo' => 'bar'))
我的理智现在正在接受超出合理措施的测试,我正在幻想对 Matt Mullenweg 的智能手机进行 DDOS 攻击
请治愈我的疯狂,并指出我明显忽略的微小细节这样我就可以继续我的一天。
简码
[subscription ID="1" referrer="overlay-promotion" buttonText="Send it to me" buttonBGHex="#000c49"]
函数
function subscription_form($atts){
//Yes, I tried extract to, and yes, I changed the variables in the html below to reflect this change.
$a = shortcode_atts( array(
"ID" => "1",
"referrer" => "not-what-you-think-this-should-be",
"buttonText" => "Subscribe",
"buttonBGHex" => "#78C138"
), $atts, "subscription" );
$html = '<form id="subscribe" name="email_signup" class="form-inline validate" action="http://www.example.com/subscribe" method="post">';
$html .= '<input type="hidden" name="referrer" value="'.$a["referrer"].'">';
$html .= '<input type="hidden" name="success_url" value="http://www.example.com/subscribe/success">';
$html .= '<input type="hidden" name="error_url" value="http://www.example.com/subscribe/error">';
$html .= '<input type="hidden" name="list_id" value="'.$a["ID"].'">';
$html .= '<input type="text" placeholder="Enter your email address" class="input-large" name="email">';
$html .= '<input type="submit" class="btn btn-primary" style="background-color:'.$a["buttonBGHex"].';" name="submit" value="'.$a["buttonText"].'">';
$html .= '</form>';
return $html;
}
add_shortcode( 'subscription', 'subscription_form' );
输出
<form id="subscribe" name="email_signup" class="form-inline validate" action="https://www.example.com/subscribe" method="post">
<input type="hidden" name="referrer" value="HP-overlay-2015">
<input type="hidden" name="success_url" value="https://www.example.com/subscribe/success">
<input type="hidden" name="error_url" value="https://www.example.com/subscribe/error">
<input type="hidden" name="list_id" value="1"><input type="text" placeholder="Enter your email address" class="input-large" name="email">
<input type="submit" class="btn btn-primary" style="background-color:#78C138;" name="submit" value="Subscribe">
</form>
根据 add_shortcode
的法典,短代码的属性名称被转换为小写。
Shortcode attribute names are always converted to lowercase before they are passed into the handler function. Values are untouched.
这意味着您需要使用小写版本访问数组键以获得正确的输出。