我正在尝试创建多个自定义用户配置文件字段,以允许我们的用户在其配置文件页面中添加更多软件技能。
我面临的问题是,它总是只为“skills"
和一个元数据"skills_percent"
即使我添加了更多的输入!
当我添加更多字段时,应该如何保存所有字段?
以及如何在前端显示它们!
这就是我所做的。
<table class="skills-table">
<tr>
<th><label for="skills"><?php _e("Skills"); ?></label></th>
<td>
<div class="multi-field-wrapper">
<button type="button" class="add-field">+</button>
<div class="multi-fields">
<div class="multi-field">
<input type="text" name="skills" id="skills" value="<?php echo esc_attr( get_the_author_meta( \'skills\', $current_user->ID ) ); ?>" class="textbox" />
<input type="text" name="skills_percent" id="skills_percent" value="<?php echo esc_attr( get_the_author_meta( \'skills_percent\', $current_user->ID ) ); ?>" class="textbox" /><br />
<button type="button" class="remove-field">-</button>
</div>
</div>
</div>
<span class="description"><?php _e("Please enter your programs or software in the first box and how many percent in the second box ."); ?></span>
</td>
</tr>
</table>
的脚本
adding/removing
字段动态。
<script type="text/javascript">
jQuery(document).ready(function($) {
$(\'.multi-field-wrapper\').each(function() {
var $wrapper = $(\'.multi-fields\', this);
$(".add-field", $(this)).click(function(e) {
$(\'.multi-field:first-child\', $wrapper).clone(true).appendTo($wrapper).find(\'input\').val(\'\').focus();
});
$(\'.multi-field .remove-field\', $wrapper).click(function() {
if ($(\'.multi-field\', $wrapper).length > 1)
$(this).parent(\'.multi-field\').remove();
});
});
});
</script>
更新用户元
if ( !empty( $_POST[\'skills\'] ) )
update_user_meta( $current_user->ID, \'skills\', esc_attr( $_POST[\'skills\'] ) );
if ( !empty( $_POST[\'skills_percent\'] ) )
update_user_meta( $current_user->ID, \'skills_percent\', esc_attr( $_POST[\'skills_percent\'] ) );
在前端显示作者元
<dt class="j-s-m">Skills</dt><dd class="text-intro"><?php echo get_the_author_meta(\'skills\', $curauth->ID); ?> <?php echo get_the_author_meta(\'skills_percent\', $curauth->ID); ?></dd>
Update 1 as mrben522答案
if ( !empty( $_POST[\'skills\'] ) )
$skillArray = [];
if (is_array($_POST[\'skills\'])) {
foreach ($_POST[\'skills\'] as $skill) {
$skillArray[] = esc_attr($skill);
}
} else {
$skillArray[] = esc_attr($_POST[\'skills\']);
}
update_user_meta( $current_user->ID, \'skills\', $skillArray );
if ( !empty( $_POST[\'skills_percent\'] ) )
$skills_percentArray = [];
if (is_array($_POST[\'skills_percent\'])) {
foreach ($_POST[\'skills_percent\'] as $skill_percent) {
$skills_percentArray[] = esc_attr($skill_percent);
}
} else {
$skills_percentArray[] = esc_attr($_POST[\'skills_percent\']);
}
update_user_meta( $current_user->ID, \'skills_percent\', $skills_percentArray );
但它会返回技能和技能百分比的数组数组,也不会保存
"multi-field"
最合适的回答,由SO网友:mrben522 整理而成
在输入名称中添加方括号,让PHP将其解释为数据数组,而不是用每个新输入覆盖值。
因此,您的输入如下所示:
<input type="text" name="skills[]" id="skills" value="<?php echo esc_attr( get_the_author_meta( \'skills\', $current_user->ID ) ); ?>" class="textbox" />
<input type="text" name="skills_percent[]" id="skills_percent" value="<?php echo esc_attr( get_the_author_meta( \'skills_percent\', $current_user->ID ) ); ?>" class="textbox" /><br />
您的消毒效果如下所示(重复以下结构
skills_percent
):
if ( !empty( $_POST[\'skills\'] ) )
$skillArray = [];
if (is_array($_POST[\'skills\'])) {
foreach ($_POST[\'skills\'] as $skill) {
if (!empty($skill)) {
$skillArray[] = esc_attr($skill);
}
}
} else {
$skillArray[] = esc_attr($_POST[\'skills\']);
}
update_user_meta( $current_user->ID, \'skills\', $skillArray );
编辑:您也可以使用
array_map
而不是foreach循环。
$skillArray = array_map(\'esc_attr\', $_POST[\'skills\']);
编辑2:下面是循环数据以显示输入的基本结构。我不喜欢这种布局,因为它依赖于两个数组的索引保持不变,但它应该可以工作。
<?php
$skills = get_the_author_meta( \'skills\', $current_user->ID );
$skillPercents = get_the_author_meta( \'skills_percent\', $current_user->ID );
if (is_array($skills) && is_array($skillPercents)) {
foreach ($skills as $index => $skill) {
?>
<div class="multi-field">
<input type="text" name="skills[]" id="skills_<?php echo $index; ?>" value="<?php echo esc_attr( $skill ); ?>" class="textbox" />
<input type="text" name="skills_percent[]" id="skills_percent_<?php echo $index; ?>" value="<?php echo esc_attr( $skillPercents[$index] ); ?>" class="textbox" /><br />
<button type="button" class="remove-field">-</button>
</div>
<?php
}
} else { ?>
<div class="multi-field">
<input type="text" name="skills[]" id="skills_0" value="<?php echo esc_attr( $skills ); ?>" class="textbox" />
<input type="text" name="skills_percent[]" id="skills_percent_0" value="<?php echo esc_attr( $skillPercents ); ?>" class="textbox" /><br />
<button type="button" class="remove-field">-</button>
</div>
<?php }
?>
您可以修改此设置,以便将每个技能和百分比保存为2d数组中的自己的数组,以使其更加坚固。