Skip Navigation

[Resolved] Update a custom field from the frontend with a click of a button

This thread is resolved. Here is a description of the problem and solution.

Problem: I have an edit post Form that allows Users to update a custom field value in a post. I would like to include multiple submit buttons in the Form, and depending on which Submit button is clicked I would like to change the custom field to a different value.

Solution: You can achieve this with 3 Forms that edit the post. Here's the general idea:
- Create an edit post Form for this post type, and auto-generate the Form contents. Delete the post title input, the post content input, and any taxonomy inputs from the Form. Delete all custom field inputs except for the select field. Set the value of the custom field to match the value corresponding to "Todo". You can find this in the custom field editor screen.
- Wrap the select field shortcode in a hidden div like this:

<div style="display:none;">
  [cred_field field='your-field-slug' value='12345' urlparam='' select_text='--- not set ---' class='form-control' output='bootstrap']
</div>

- Change the value of the "Submit" button shortcode to be "Todo".
- Place this Form in the template for the post you want to edit.
- Now all that should be visible in this Form is the "Todo" button. When the User submits this Form, the select field will be set with the correct status. None of the other post information will be changed.
- Repeat the process for the two other Forms.
- Once your buttons are all visible, use conditional HTML to show and hide the Forms as needed depending on the post's current custom field value. In other words, hide the "Todo" button when the custom field value is already "Todo", etc.

This support ticket is created 6 years, 2 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 4 replies, has 2 voices.

Last updated by haryonoS 6 years, 2 months ago.

Assisted by: Christian Cox.

Author
Posts
#1106570

I would like to be able to change a custom field value of a custom post type from the frontend with a click of a button.

For example, I have a custom post type "task" with a custom field "progress" which is a select type with three values: "todo", "doing", and "done".

In the layout for "task", I would like to add three buttons: "Todo", "Doing", and "Done", to set the current task status according to which button is clicked.

How can I achieve this?

#1106804

You can achieve this with 3 Forms that edit the post. Here's the general idea:
- Create an edit post Form for this post type, and auto-generate the Form contents. Delete the post title input, the post content input, and any taxonomy inputs from the Form. Delete all custom field inputs except for the select field. Set the value of the custom field to match the value corresponding to "Todo". You can find this in the custom field editor screen.
- Wrap the select field shortcode in a hidden div like this:

<div style="display:none;">
  [cred_field field='your-field-slug' value='12345' urlparam='' select_text='--- not set ---' class='form-control' output='bootstrap']
</div>

- Change the value of the "Submit" button shortcode to be "Todo".
- Place this Form in the template for the post you want to edit.
- Now all that should be visible in this Form is the "Todo" button. When the User submits this Form, the select field will be set with the correct status. None of the other post information will be changed.
- Repeat the process for the two other Forms.
- Once your buttons are all visible, use conditional HTML to show and hide the Forms as needed depending on the post's current custom field value. In other words, hide the "Todo" button when the custom field value is already "Todo", etc.

#1106811

Hi, Christian.

I get the idea and will get back to you with the result once I try this.

Thanks!

#1106846

Sounds good, I'll stand by for your update. No need to reply right now. The ticket will stay open for 30 days.

#1109087

I just want to let you know that your solution works. A bit tricky but I'm quite happy with the result.

Thanks!