Skip Navigation

[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.

You can find the proposed solution in this case with the following reply:
https://toolset.com/forums/topic/how-to-remove-tags-when-saving-a-post-with-a-form/#post-1517791

Relevant Documentation:
=> https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
=> https://developer.wordpress.org/reference/functions/wp_set_object_terms/

This support ticket is created 4 years, 10 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 7 replies, has 2 voices.

Last updated by ericE-4 4 years, 10 months ago.

Assisted by: Minesh.

Author
Posts
#1515791

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?

#1516025

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Do you mean with for post type for which you created the form you assigned a WordPress default taxonomy tags?

Can I have a problem URL to see your form and a screenshot of your post form from backend?

#1516043

Yes, that's exactly what I mean. No problem URL to give you, I need to know how to do it before I attempt it and find out if there's a problem.

#1516073

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

There are a couple of ways to do that.

Either you should use the Form's hook cred_before_save_data and unset the post_tag value you want to clear.
=> https://toolset.com/documentation/programmer-reference/cred-api/#cred_before_save_data

OR

You can use the another hook that runs after the post is saved cred_save_data and use the wp_set_object_terms() function to set post_tag to null.
=> https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
=> https://developer.wordpress.org/reference/functions/wp_set_object_terms/

For example:

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.

#1517277

I added the code as follows:

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."

#1517735

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

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.

#1517791

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

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.

#1517795

Great, thanks it seems to work great. My issue is resolved now. Thank you!