Tell us what you are trying to do?
I have a CRED form. If the custom select field "Event" is equal to 2, then nothing should happen and the post should be saved as 'appointment' post type. However if the custom select field "Event" is equal to 1, then post type should be changed into 'event' post type. I use cred_save_data hook and set_post_type()
The good thing is, that after form submit and under the condition, that the custom select field "Event" is equal to 1, the post type of the newly submitted post changes to 'event'.
The bad thing is, that I receive a critical WordPress error message after the form submission. It won't redirect me to the original page. WordPress doesn't seem to like the combination of "cred_save_data hook and set_post_type()" ...
How can I avoid the error message?
Code below..
add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data) {
$forms = array( 123 );
if ( in_array( $form_data['id'], $forms ) )
{
if ($_POST['wpcf-event']['select'] == "1")) {
set_post_type( $post_id, $post_type = 'event' );
}
}
}
Is there any documentation that you are following?
no
Is there a similar example that we can see?
no
What is the link to your site?
Site under development
Hello, I see some potential issues in the custom code.
1. There is an extra closing parenthesis on line 7, it should be deleted.
2. If the event field is a select field created by Types, you should test the selected value like this:
if ($_POST['wpcf-event'] == "1") {
This nested array element does not exist, and will throw an error:
$_POST['wpcf-event']['select']
3. Be sure to change 123 in your custom code to match this Form ID. It looks like you may have some test information here, just want to be sure you have the correct Form ID number in your actual code.
4. The correct syntax for set_post_type is as follows:
set_post_type( $post_id, 'event' );
Don't pass a variable assignment in the second param, just the post type slug as a string.
Try those adjustments and let me know the results, I may need to take another look at this.
Hi Christian, many thanks for your answer.
I changed everything according to your suggestions.
add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data) {
$forms = array( 10440 );
if ( in_array( $form_data['id'], $forms ) {
if ($_POST['wpcf-event'] == "1")) {
set_post_type( $post_id, 'event' );
}
}
}
It performs the action that it should perform (change the post type), but it still throws the error message... Could it be that cred_save_data is the wrong hook?
I think you removed the wrong closing parenthesis. Here is your code:
add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data) {
$forms = array( 10440 );
if ( in_array( $form_data['id'], $forms ) {
if ($_POST['wpcf-event'] == "1")) {
set_post_type( $post_id, 'event' );
}
}
}
Here is the corrected code:
add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data) {
$forms = array( 10440 );
if ( in_array( $form_data['id'], $forms ) ) {
if ($_POST['wpcf-event'] == "1") {
set_post_type( $post_id, 'event' );
}
}
}
Yes you are right. I accidently removed the wrong closing parenthesis. I changed it, but the problem persist.
I even tried:
add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data) {
$forms = array( 10440 );
if ( in_array( $form_data['id'], $forms ) ) {
set_post_type( $post_id, 'event' );
}
}
I was curious, so I also tried:
add_action('cred_submit_complete', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data) {
$forms = array( 10440 );
if ( in_array( $form_data['id'], $forms ) ) {
set_post_type( $post_id, 'event' );
}
}
... and it works 🙂
Many thanks for your suggestions Christian.
My issue is resolved now. Thank you!
add_action('cred_submit_complete', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data) {
$forms = array( 10440 );
if ( in_array( $form_data['id'], $forms ) ) {
set_post_type( $post_id, 'event' );
}
}