This thread is resolved. Here is a description of the problem and solution.
Problem:
The issue here is that the user wanted to add a status option to their frontend form so that the post can be saved as a draft or publish based on the user's choice.
Solution:
This is possible but you will need to do it using a select field. Create a generic select field like the one below on your form and set the statuses you want the user to choose from.
The user will then select the status they want to submit the post as.
From there we can use a Cred hook to hook into the save post and then set the status there.
add_action('cred_submit_complete', 'my_success_action',10,2);
function my_success_action($post_id, $form_data)
{
// if a specific form
if ($form_data['id']==346)
{
if (isset($_POST['order_stat']))
{
// add it to saved post meta
$my_post = array(
'ID' => $post_id,
'post_status' => $_POST['order_stat'],
);
wp_update_post($my_post);
}
}
}
Now you will replace the my_custom_field with the slug of the generic field that you made. Note it would be best if this was a select field where the value and the display name is also the same.
This support ticket is created 5 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.
hey,
Is it possible to create a "save as draft" button in a cred form? that is - have 2 buttons, 1 will publish the post (and initiate notifications etc), and the other just save the draft.
I thought about duplicating the form, then having the duplicate save drafts instead of publishing, then placing both forms in one page and hide the duplicate (except for the submission button).
Is this doable?
Or is there another way around?
thanks!
Actually this one may be possible but maybe not with 2 submit buttons. I would suggest adding a generic field to the form that contains the status you want i.e published and draft.
The user will then select the status they want to submit the post as.
From there we can use a Cred hook to hook into the save post and then set the status there.
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']==12)
{
if (isset($_POST['my_custom_field']))
{
// add it to saved post meta
$my_post = array(
'ID' => $post_id,
'post_status' => $_POST['my_custom_field'],
);
}
}
}
Now you will replace the my_custom_field with the slug of the generic field that you made. Note it would be best if this was a select field where the value and the display name is also the same.
I tried - but it didn't work.
In functions.php I added:
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']==346)
{
if (isset($_POST['order_stat']))
{
// add it to saved post meta
$my_post = array(
'ID' => $post_id,
'post_status' => $_POST['order_stat'],
);
}
}
}
nope, still doesn't work.
i tried changing the cred form to set post status to "draft" - but then it saves it only as drafts, and not as published (if i choose "published" from the select field).
by the way - shouldn't the status be "publish" and not "published"?
thx!
Hey Shane,
No, it doesn't work unfortunately..
The cred form saves according to the "set status" setting in the form, regardless of the generic field.
Ido
I think the issue might be that we are hooking into the post too early to update it's status.
Could you try it with this hook .
add_action('cred_submit_complete', 'my_success_action',10,2);
function my_success_action($post_id, $form_data)
{
// if a specific form
if ($form_data['id']==346)
{
if (isset($_POST['order_stat']))
{
// add it to saved post meta
$my_post = array(
'ID' => $post_id,
'post_status' => $_POST['order_stat'],
);
}
}
}
This hook fires after the post has been saved to the database so it's quite possible that this one might be able to change the post status.
My apologies It seems I made an error and forgot to put the line to actually update the post itself.
This code should work fine now.
add_action('cred_submit_complete', 'my_success_action',10,2);
function my_success_action($post_id, $form_data)
{
// if a specific form
if ($form_data['id']==346)
{
if (isset($_POST['order_stat']))
{
// add it to saved post meta
$my_post = array(
'ID' => $post_id,
'post_status' => $_POST['order_stat'],
);
wp_update_post($my_post);
}
}
}