Home › Toolset Professional Support › [Resolved] Manage native wordpress post status in cred form
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)
Tagged: Toolset Forms
This topic contains 27 replies, has 2 voices.
Last updated by virageguitars 7 years, 2 months ago.
Assisted by: Minesh.
Hi, everybody,
Can I manage the native wordpress post status with a CRED form? What the control I have to insert in form for this?
Thanks to all for your time 🙂
Hello. Thank you for contacting the Toolset support.
As I understand you would like to change the post status posts using CRED forms - correct?
You can use CRED hook: cred_save_data
add_action('cred_save_data', 'change_parent_status',10,2); function change_parent_status($post_id, $form_data){ if ($form_data['id']==9999){ $my_post = array( 'ID' => $post_id, 'post_status' => 'draft' ); wp_update_post($my_post); } }
Where:
Replace 9999 with your CRED form ID.
More info:
=> https://toolset.com/documentation/user-guides/cred-api/#csd
Hi, Minesh,
Thanks for your quick reply.
Yes, I'd like to change the post status posts using CRED forms. I'd like to have some control (drop down or another select) in cred form by which I can change CPT status (native wordpress post status)
When you say native wordpress post status - do you mean to use default post type "post" - correct?
1)
Create a CRED edit form for your post type and add a a drop down button "post_status" in your CRED form using generic field:
[cred-generic-field field="post_status" type="select" class=""] { "required":0, "validate_format":0, "default":["publish"], "options":[ {"value":"draft","label":"draft"}, {"value":"publish","label":"publish"} ] }
2)
Use CRED action hook "cred_save_data" to change the post status after user submit the form
add_action('cred_save_data', 'change_post_status',10,2); function change_post_status($post_id, $form_data) { if ($form_data['id']==9999){ $my_post['ID'] = $post_id; $my_post['post_status'] = $_REQUEST['post_status']; // Update the post into the database wp_update_post( $my_post ); } }
Where:
Replace 9999 with your CRED form ID.
More help: Function Reference/wp update post
http://codex.wordpress.org/Function_Reference/wp_update_post
There are no errors?
... {"value":"draft","label":"draft"}, {"value":"publish","label":"publish"} ...
What you mean by error. Please find blueprint of CRED gneneric field shortcode.
[cred_generic_field field='myselectfield' type='select' class='' urlparam=''] { "required":1, "validate_format":0, "persist":0, "default":["0"], "options":[ {"value":"0","label":"Select"}, {"value":"1","label":"one"}, {"value":"2","label":"two"}, {"value":"3","label":"three"} ] } [/cred_generic_field]
More info:
=> https://toolset.com/documentation/user-guides/cred-shortcodes/#cred_generic_field
What does it mean "my_post" ?
I can change this param to my CPT post?
Well - $my_post is a variable.
If you do not know PHP let me know I can help you. Please share your exact requirements and problem URL.
*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.
I would additionally need your permission to de- and re-activate Plugins and the Theme, and to change configurations on the site. This is also a reason the backup is really important. If you agree to this, please use the form fields I have enabled below to provide temporary access details (wp-admin and FTP).
I have set the next reply to private which means only you and I have access to it.
My CPT has name "event". Can you show me the right code with this CPT name? Thanks
Well - to which content type your CRED form belongs to? Are you using CRED form?
Could you please show me your requirement or problem URL how you would like to change your post status?
I have CPT 'event'.
I have CRED form for create this CPT (default status is ''draft).
I have another CRED form for edit this CPT 'event'
I'd like to change post status with this edit CRED form.
"Could you please show me your requirement or problem URL how you would like to change your post status?" - I'd like to insert for this possibility a select field with the list of statuses (wordpress native statuses, eg draft, private, published etc)
How I can do this?
Thanks again 🙂
1)
Add following code to your Edit CRED form:
[cred_generic_field field='my_post_status' type='select' class='' urlparam=''] { "required":1, "validate_format":0, "persist":0, "default":["0"], "options":[ {"value":"0","label":"Select"}, {"value":"draft","label":"draft"}, {"value":"publish","label":"publish"}, {"value":"private","label":"private"}, ] } [/cred_generic_field]
2)
Add following code to your current theme's functions.php file.
add_action('cred_save_data', 'change_post_status',10,2); function change_post_status($post_id, $form_data) { if ($form_data['id']==9999){ $my_post['ID'] = $post_id; $my_post['post_status'] = $_REQUEST['my_post_status']; // Update the post into the database wp_update_post( $my_post ); } }
Where:
Replace 9999 with your CRED form ID.
As your CRED form is attached to event post type there is not need. You should just copy past the above code to described locations and try to resolve your issue.
Made it. I don't see any selector control in my CRED form 🙁
well - I need to check on your install why the field is not rendering.
Please share problem URL.
*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.
I would additionally need your permission to de- and re-activate Plugins and the Theme, and to change configurations on the site. This is also a reason the backup is really important. If you agree to this, please use the form fields I have enabled below to provide temporary access details (wp-admin and FTP).
I have set the next reply to private which means only you and I have access to it.
Dear Minesh,
please inform me just as soon as you resolve this issue.
Thank you very much for your help 🙂