[Resolved] How to remove tags when saving a post with a form?
This thread is resolved. Here is a description of the problem and solution.
Problem:
How to remove specific tags assigned to post when saving a post with a form?
Solution:
You can use the Toolset Form hook that runs after the post is saved cred_save_data and use the wp_remove_object_terms() function to delete the specific post_tag.
I know there's a tags field type that allows users to add tags, but I need to delete a tag when the post is saved. The post will have a the tag "pending" when the user first edits the form, and when it's saved, I need to remove that tag (while keeping any other tags it may have intact). Is there a way to do this?
add_action('cred_save_data', 'func_clear_tags',10,3);
function func_clear_tags($post_id, $form_data) {
// if a specific form
if ($form_data['id']==9999) {
wp_set_object_terms( $post_id, null, 'post_tag' );
}
}
Where:
- Replace 9999 with your original form ID.
- change 'post_tag' with your original taxonomy slug, if required.
add_action('cred_save_data', 'func_clear_tags',10,3);
function func_clear_tags($post_id, $form_data) {
// if a specific form
if ($form_data['id']==6671) {
wp_set_object_terms( $post_id, null, 'pending' );
}
}
However it did not work. I tried it with a post that had the tags "pending" and "partner badge". After submitting the form both tags were still there.
Perhaps the other option you mentioned ("use the Form's hook cred_before_save_data") is the way to go? But I don't know how to "unset the post_tag value you want to clear."
That is why I asked to share the problem URL where I can see the form as well as access details so I can share with you the working solution and the most relevant applicable solution.
Without knowing your form structure and flow its hard for me to guide you in the right direction.
Can you please send me the required access details with the problem URL.
As you are using the Post Edit form, you need to use the function: wp_remove_object_terms() to remove the specific term assigned to the post you are editing.
I've adjusted the code as given under:
add_action('cred_save_data', 'func_clear_tags',10,3);
function func_clear_tags($post_id, $form_data) {
// if a specific form
if ($form_data['id']==6671) {
wp_remove_object_terms( $post_id, 'pending', 'post_tag' );
}
}
I can see it works as expected. Can you please confirm.