Skip Navigation

[Résolu] [CRED] How to assign a post type to form programmatically?

Ce fil est résolu. Voici une description du problème et la solution proposée.

Problem:
Is there any way that I can assign the same form, to different post types? either from backend or frontend, programmatically?

Solution:

Just add a parameter like "?division=something" to your link where the form is eg. http://www.site.com/mypage/?division=europe, then add this function to your functions.php:

function cred_change_cpt($post_id, $form_data) {
    $division_form = $_GET['division'];
    if ($form_data['id'] == 16 && $division_form != '') {
        $my_post = array(
                      'ID'           => $post_id,
                      'post_type'   => 'rate-plan-'.$division_form
        );
           wp_update_post( $my_post );
    }
}
add_action('cred_save_data', 'cred_change_cpt',10,2);
This support ticket is created Il y a 8 années et 2 mois. 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 8:00 – 17:00 8:00 – 17:00 8:00 – 17:00 8:00 – 17:00 8:00 – 17:00 -
- - - - - - -

Supporter timezone: America/Sao_Paulo (GMT-03:00)

This topic contains 10 réponses, has 3 voix.

Last updated by Sanny Il y a 8 années et 2 mois.

Assisted by: Adriano.

Auteur
Publications
#364147

Hello,
I can see that a CRED form asks for post type upon form creation, hence that form can now only be used for that post type only (and needs to be cloned in case I want the same exact form for other post types).

Is there any way that I can assign the same form, to different post types? either from backend or frontend, programmatically?

I have the same exact custom fields and taxonomies, for 5 different post types.
So right now, I would have to duplicate and mantain 5 different forms (even though they are identical inside).

Is there a way I can simplify this, keep 1 form and maybe add a parameter to cred form shortcode, to assign a different post type, depending on frontend page?
I would also be open to use View to achieve this (to a certain extent), even though I wouldnt be too happy about it 🙂

#364164

Thank you for contacting Toolset support, I'd be delighted to assist!

I checked our documentation on how to accomplish this and we do not yet have a published API existing.

I have sent this to our Tier 2 Supporters to explore the possibility with our developers or if there is an internal API we could hook into!

If all else fails, going through the CRED code will show you how we assign CRED forms to CPTs.

#364195

Thank you, I'll be waiting for their answer then.
Meanwhile, could you please point out to the relevant file/s and lines in CRED?

#364494

hello?

#364510

Thank you for your patience! I am not a Tier 2 Supporter or Toolset Developer and as such, I would know as much as you as to where the internal API function names are within the Toolset source code. If you cannot wait for Tier 2 to answer the ticket, you may search the files, they can be downloaded from here: https://toolset.com/account/downloads/

#365737

still nothing?
going through the whole CRED plugin isnt very practical... it would be nice if you could at least provide some guidance as to which files... so I'll wait for paid support...

#366941

It's been 2 weeks now.... still no support??

#367509

Hello,

I’m Adriano, Toolset support lead and I have been escalated to this thread. I’ll give my best to help you to achieve your needs through Toolset components.

My apologies the long delay. You can change the post type through the CRED api:
https://toolset.com/documentation/user-guides/cred-api/#cbsd

One example below:

add_action('cred_before_save_data', 'my_before_save_data_action',10,1);
function my_before_save_data_action($form_data)
{
    // if a specific form
    if ($form_data['id']==12)
    {
        $form_data['post_type'] = 'my_new_post_type';
    }
}

Please let me know if you are satisfied with my reply and any other questions you may have.

Regards,

Adriano Ferreira

#367744

That seems to work, but the post is still saved as the default post type afterwards:
(default post type name in the CRED form is "rate-plan", the one I am trying to assigning is "rate-plan-ap")

var_dump($form_data);
array(4) { ["id"]=> int(16) ["post_type"]=> string(12) "rate-plan-ap" ["form_type"]=> string(3) "new" ["container_id"]=> int(13237) } 

While that seems to change the post_type of the form, so we are partially achieving the request... inside the form, there are still fields like:

[cred_field field="post_title" post="rate-plan" value="" urlparam=""] 

Clearly that post="rate-plan" is still going to try and modify the default post type, not the new one... so in the end, the post is still saved as the default form post type.

Is there a way to dynamically change post="rate-plan", inside the form for all the fields, depending on a certain value? (eg. if is page name is "Add New Form", leave default "rate-plan", but if page name is "AP New Post Form" then replace post="rate-plan" with post="rate-plan-ap").

Maybe with JS? or a custom CRED short-code?
Or could we completely remove post="rate-plan" inside the CRED fields? (not sure how important it is, if we are already forcing a post_type to the form anyway)

#368029

Oh, sorry! It was totally my mistake, those hooks are actions and do not return anything to CRED, it doesn't modify nothing in the form before submitting. It should be used to update some custom field or do something before saving the post, but you can't change nothing regarding this form.

To achieve what you want you could use the cred_save_data: https://toolset.com/documentation/user-guides/cred-api/#csd

Together with the wp_update_post from WP: https://codex.wordpress.org/Function_Reference/wp_update_post

So you can modify the post after it is saved on database.

Please let me know if you are satisfied with my reply and any other questions you may have.

Regards,

Adriano Ferreira

#368185

For anyone looking for a solution to this...
Just add a parameter like "?division=something" to your link where the form is eg. hidden link, then add this function to your functions.php:

function cred_change_cpt($post_id, $form_data) {
	$division_form = $_GET['division'];
   	if ($form_data['id'] == 16 && $division_form != '') {
		$my_post = array(
	                  'ID'           => $post_id,
	                  'post_type'   => 'rate-plan-'.$division_form
	  	);
	       wp_update_post( $my_post );
    }
}
add_action('cred_save_data', 'cred_change_cpt',10,2);
This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.