【发布时间】:2020-03-17 06:50:30
【问题描述】:
我有一个前端帖子,允许用户更新他们的帖子。目前我正在使用它来允许输入新标签:
这是我的 HTML 表单:
<form id="featured_upload" method="post" action="#" enctype="multipart/form-data">
<input type="file" name="new_image_upload" id="new_image_upload" multiple="false" />
<input type="text" name="newtags" id="newtags" value="" />
<input type="hidden" name="post_id" id="post_id" value="" />
<?php wp_nonce_field( 'new_image_upload', 'new_image_upload_nonce' ); ?>
<input id="submit_new_image_upload" name="submit_new_image_upload" type="submit" value="Upload" />
</form>
这是代码的一部分:
// If the form has been submitted with new tags
if ( isset( $_POST['newtags'] ) ) {
// get existing tags
$post_tags = get_the_tags();
// concatenates both existing and new tags
$concatenated_tags = array($post_tags, sanitize_text_field($_POST['newtags']));
// Add all the tags to the post
wp_set_post_tags(get_the_ID(), $concatenated_tags , true );
}
这样做的问题是在我的数据库上创建了许多不必要的标签,我需要我的用户看到我的数据库中已经存在的标签的建议。
所以,我需要 <input type="text" name="newtags" id="newtags" value="" /> 让它们成为现有的标签建议,就像 wordpress 在后期编辑后端所做的那样。请参阅下图以在我的前端表单上检查所需的结果:
【问题讨论】: