Tell us what you are trying to do?
I'd like to have two buttons in am AJAX CRED post form : "Submit" and "Save". The latter saves the post as a draft and the former publishes it.
I referenced this support thread (https://toolset.com/forums/topic/possibility-of-save-and-also-send-and-close-button-in-content-form/), and was able to create two submit buttons, and use this code to save it as a draft:
function my_save_continue($post_id, $form_data) {
if($form_data['id']==41355) {
if(isset($_POST['form_submit_2']) && $_POST['form_submit_2'] == 'Save') {
$my_post['ID'] = $post_id;
$my_post['post_status'] = 'draft';
wp_update_post( $my_post );
}
}
}
add_action('cred_save_data', 'my_save_continue',10,2);
However, this didn't work on an AJAX form.
$_POST contains form_submit_2 in a non-AJAX form:
[form_submit_2] => Save
[_cred_cred_wpnonce_cred_form_41355] => b240fef17a
[_cred_cred_prefix_post_id] => 41713
[_cred_cred_prefix_cred_container_id] => 41360
[_cred_cred_prefix_form_id] => 41355
[_cred_cred_prefix_form_count] => 1
But not in an AJAX form:
[_cred_cred_wpnonce_cred_form_41355] => b240fef17a
[_cred_cred_prefix_post_id] => 41710
[_cred_cred_prefix_cred_container_id] => 41360
[_cred_cred_prefix_form_id] => 41355
[_cred_cred_prefix_form_count] => 1
[action] => cred_ajax_form
[form_submit] => true
Is the code I'm using the code you recommend for saving the post as a draft, and is there a direct way to get it working using AJAX, considering $_POST doesn't have form_submit_2 in an AJAX form?
Thanks.
Hello,
There isn't such kind of built-in feature within Toolset forms plugin, and in that thread you mentioned above, that user does not use AJAX submitting form.
In your case, you will need to consider other custom codes, for example:
1) Edit your post form, in section "Content", add a hidden generic field "post_status" just below the submit buttons:
[cred_generic_field type='hidden' field='post_status']
{
"default":"0"
}
[/cred_generic_field]
2) Click "JS Editor", add below JS codes:
jQuery(function($) {
$('input[name="form_submit_2"]').click(function(e){
$('input[name="post_status"]').attr('value', 1);
console.log( "ready!" );
});
});
So when user click the "Save" button (form_submit_2), it will change the hidden field "post_status" value to 1,
3) Modify your PHP codes to check the hidden field "post_status" value, like this:
add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
// if a specific form
if ($form_data['id']==41355)
{
if (isset($_POST['post_status']) && $_POST['post_status'] == '1')
{
$my_post['ID'] = $post_id;
$my_post['post_status'] = 'draft';
wp_update_post( $my_post );
}
}
}
Thanks, that worked.
We also want to redirect to another page on our website after it is saved as a draft. How to do that? Is it done in the same hook or another one?
For the new question:
We also want to redirect to another page on our website after it is saved as a draft.
I suggest try another filter hook of Toolset form plugin: cred_success_redirect
See our document:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_success_redirect
If you need more assistance for it, new ticket please.