简体   繁体   English

WordPress-将标签添加到现有帖子,前端

[英]Wordpress - Add a tag to an existing post, front-end

I'm been researching this all day and haven't found an elegant solution, what I'm trying to do is tag a user id onto an existing post on the front end (Using Wordpress). 我整天都在研究,还没有找到一个优雅的解决方案,我想做的是将用户ID标记到前端的现有帖子上(使用Wordpress)。 The process will be ad follows... 该过程将遵循以下步骤...

  • The post has a text input and submit button (front end). 该帖子具有文本输入和提交按钮(前端)。
  • The user (registered), enters a tag in the text field and submits 用户(注册),在文本字段中输入标签并提交
  • The tag is written to the post, (ideally without a refresh using ajax but not essential.) 标记被写入帖子中(理想情况下不使用ajax刷新但不是必需的。)

It seems like it should be straight forward but I can't find any related info, thanks in advance. 似乎应该很简单,但是我找不到任何相关信息,谢谢。

EDIT: Working, final Code looks like this, thanks! 编辑:工作,最终代码看起来像这样,谢谢!

<form name="primaryTagForm" id="primaryTagForm" method="POST" enctype="multipart/form-data" >
    <fieldset>
        <input type="text" name="newtags" id="newtags" value="true" />
        <?php wp_nonce_field( 'post_nonce', 'post_nonce_field' ); ?>
        <button class="button" type="submit"><?php _e('Tag Product', 'framework') ?></button>
    </fieldset>
</form>

<?php 
    // If a user is logged in, and a form has been submitted with new tags
    if ( is_user_logged_in() && isset( $_POST['newtags'] ) ) {
    // Add the tags
    wp_set_post_tags(get_the_ID(), sanitize_text_field($_POST['newtags']), true    );
} ?>

Your question is way too broad and would require someone to build an entire form (including processing logic) for you. 您的问题范围太广,需要有人为您构建完整的表单(包括处理逻辑)。 That's not what StackOverflow is for; 那不是StackOverflow的目的。 so I'll answer the simple question that exists in your post title: 因此,我将回答您的帖子标题中存在的简单问题:

How to add a tag to an existing post, from the front-end 如何从前端向现有帖子添加标签

Answer: 回答:

Use wp_set_post_tags() to add tags to a post, when the form is processed. 处理表单后,使用wp_set_post_tags()将标签添加到帖子中。 For example, if your form input is named newtags , you could use something like: 例如,如果您的表单输入名为newtags ,则可以使用如下代码:

// If a user is logged in, and a form has been submitted with new tags
if ( is_user_logged_in() && isset( $_POST['newtags'] ) ) {
    // Add the tags
    wp_set_post_tags(get_the_ID(), sanitize_text_field($_POST['newtags']), true );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM