我设置了一个包含标准php/html表单文件的短代码。在表单中,我有一个隐藏字段,其中的值是一个变量,该变量意味着可以通过shortcode属性在表单中使用。
问题是clientId的值不是从以下位置传递的:
[insert-form form_location="form.php" clientId="25052"]
相反,我得到的是定义为默认值(NULL)的值。请参见下面的代码。
短代码:
function insert_the_form($atts){
$form_base = plugin_dir_path(__DIR__);
// Shortcode attributes & options
$atts = shortcode_atts(
array(
\'form_location\' => \'NULL\',
\'clientId\' => \'NULL\' //Variable for client ID
), $atts, \'cm_insert_cmform\'
);
if ( isset($atts[\'form_location\']) ){
$form_location = $atts[\'form_location\'];
}
if ( isset($atts[\'clientId\']) ){
$clientId = $atts[\'clientId\'];
}
ob_start();
include($form_base . \'forms/\' . $form_location);
$out = ob_get_clean();
return $out;
}
add_shortcode( \'insert-form\', \'insert_the_form\' );
表格:
<form id="aform" method="post" action="/">
<input type="hidden" name="clientid" id="clientid" value="<?php echo $clientId; ?>" />
<input type="submit" id="submitButton" value="Submit" class="button" />
</form>
最合适的回答,由SO网友:greg 整理而成
好吧,不知怎么的,我把它改成了这样:它看起来不像“the\\u clientId”。也许它只是不喜欢大写字母????
[insert-form form_location="form.php" identification_number="12345"]
function insert_the_form($atts){
$form_base = plugin_dir_path(__DIR__);
// Shortcode attributes & options
$atts = shortcode_atts(
array(
\'form_location\' => \'NULL\',
\'identification_number\' => \'NULL\' //Variable for client ID
), $atts, \'insert_the_form\'
);
if ( isset($atts[\'form_location\']) ){
$form_location = $atts[\'form_location\'];
}
if ( isset($atts[\'identification_number\']) ){
$form_idnumber = $atts[\'identification_number\'];
}
$clientId = $form_idnumber;
ob_start();
include($form_base . \'forms/\' . $form_location);
$out = ob_get_clean();
return $out;
}
add_shortcode( \'insert-form\', \'insert_the_form\' );