Skip Navigation

[Resuelto] allowing «save as draft»

Este hilo está resuelto. Aquí tiene una descripción del problema y la solución.

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.


   [cred_generic_field type='select' field='order_stat' class='order-stat']
{
"required":0,
"default":["draft"],
"options":[{"value":"draft","label":"draft"},{"value":"publish","label":"publish"}]
}
[/cred_generic_field]

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 hace 5 años, 2 meses. 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.

Hoy no hay técnicos de soporte disponibles en el foro Juego de herramientas. Siéntase libre de enviar sus tiques y les daremos trámite tan pronto como estemos disponibles en línea. Gracias por su comprensión.

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

Supporter timezone: America/Jamaica (GMT-05:00)

Etiquetado: 

Este tema contiene 10 respuestas, tiene 2 mensajes.

Última actualización por Ido Angel hace 5 años, 2 meses.

Asistido por: Shane.

Autor
Mensajes
#1336711

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!

#1336799

Shane
Supporter

Idiomas: Inglés (English )

Zona horaria: America/Jamaica (GMT-05:00)

Hi Ido,

Thank you for getting in touch.

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.

Please let me know if this helps.
Thanks,
Shane

#1336885

Hey Shane!

Thanks 🙂

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'],
  );
        }
    }
}

and in the form (with ID 346) I added:

    [cred_generic_field type='select' field='order_stat' class='order-stat']
{
"required":0,
"default":["draft"],
"options":[{"value":"draft","label":"draft"},{"value":"published","label":"published"}]
}
[/cred_generic_field]

But still, if I choose "draft" (which is the default), the post gets published.
What am I missing?

Thx!

#1336953

Shane
Supporter

Idiomas: Inglés (English )

Zona horaria: America/Jamaica (GMT-05:00)

Hi Ido,

Could you try adding "persist":1

To the generic field ?

Thanks,
Shane

#1337009

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!

#1337023

Shane
Supporter

Idiomas: Inglés (English )

Zona horaria: America/Jamaica (GMT-05:00)

Hi Ido,

Yes the status should be "publish" and not "published".

The draft status works fine right ?

Please let me know if this helps.
Thanks,
Shane

#1337025

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

#1337877

Shane
Supporter

Idiomas: Inglés (English )

Zona horaria: America/Jamaica (GMT-05:00)

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

Please let me know if it helps.
Thanks,
Shane

#1337931

still no, unfortunately :\

#1338125

Shane
Supporter

Idiomas: Inglés (English )

Zona horaria: America/Jamaica (GMT-05:00)

Hi Ido,

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);
        }
    }
}

This should work now.

Thanks,
Shane

#1338235

Perfect, Shane! Thanks!