Skip Navigation

[Resolved] Custom Post Type Form

This support ticket is created 5 years, 1 month 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.

Sun Mon Tue Wed Thu Fri Sat
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

This topic contains 5 replies, has 2 voices.

Last updated by Beda 5 years, 1 month ago.

Assisted by: Beda.

Author
Posts
#1464075
detailed-error-message.png

Hello,

We have a question regarding our create post (Condition and Treatment) forms that we have built with Toolset. We are receiving an error message when a user tries to submit the form (please see attached screenshot). Any help in resolving this matter would be greatly appreciated!

More details about our overall project can be found here - https://toolset.com/forums/topic/adding-several-custom-fields/

Condition code snippet -

<?php
/**
 * Process condition relationships with providers
 */

toolset_snippet_security_check() or die( 'Direct access is not allowed' );

// Put the code of your snippet below this comment.
// function to process condition relationships with providers
add_action('cred_save_data', 'condition_processing_action',10,2);
function condition_processing_action($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==2217)
    {
        // delete the existing connection between provider and condition
            // get existing connection
            $existing_connections = toolset_get_related_posts($post_id,'condition-provider','child');
            if(!empty($existing_connections)) {
                // delete the found connection
                toolset_disconnect_posts( 'condition-provider', $existing_connections[0], $post_id );      
            }
 
        // if any option for a condition is selected connect it to the provider
            // if any option for a condition is selected
            if(!empty($_POST['select-condition'])) {
                // connect the selected condition with the provider
                toolset_connect_posts( 'condition-provider', $_POST['select-condition'], $post_id );
            }  
    }
}

Treatment code snippet -

<?php
/**
 * Process treatment relationships with providers
 */

toolset_snippet_security_check() or die( 'Direct access is not allowed' );

// Put the code of your snippet below this comment.
// function to process treatment relationships with providers
add_action('cred_save_data', 'treatment_processing_action',10,2);
function treatment_processing_action($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==2217)
    {
        // delete the existing connection between provider and treatment
            // get existing connection
            $existing_connections = toolset_get_related_posts($post_id,'treatment-provider','child');
            if(!empty($existing_connections)) {
                // delete the found connection
                toolset_disconnect_posts( 'treatment-provider', $existing_connections[0], $post_id );      
            }
 
        // if any option for a treatment is selected connect it to the provider
            // if any option for a treatment is selected
            if(!empty($_POST['select-treatment'])) {
                // connect the selected treatment with the provider
                toolset_connect_posts( 'treatment-provider', $_POST['select-treatment'], $post_id );
            }  
    }
}

Subspecialty (working) code snippet -

<?php
/**
 * Process subspecialty relationships with providers
 */

toolset_snippet_security_check() or die( 'Direct access is not allowed' );

// Put the code of your snippet below this comment.
// function to process subspecialty relationships with providers
add_action('cred_save_data', 'subspecialty_processing_action',10,2);
function subspecialty_processing_action($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==2211)
    {
        // delete the existing connection between provider and subspecialty
            // get existing connection
            $existing_connections = toolset_get_related_posts($post_id,'subspecialty-provider','child');
            if(!empty($existing_connections)) {
                // delete the found connection
                toolset_disconnect_posts( 'subspecialty-provider', $existing_connections[0], $post_id );      
            }
 
        // if any option for a subspecialty is selected connect it to the provider
            // if any option for a subspecialty is selected
            if(!empty($_POST['select-subspecialty'])) {
                // connect the selected subspecialty with the provider
                toolset_connect_posts( 'subspecialty-provider', $_POST['select-subspecialty'], $post_id );
            }  
    }
}

Code within View to show all 3 forms -

<h2>Specialty</h2>
[cred_form form='form-to-edit-a-provider-select-specialty' post='[wpv-post-id]']
[wpv-conditional if="( '[wpv-post-id item='@specialty-provider.parent']' ne '' )"]
<h2 style="padding-top:40px;">Subspecialty</h2>
[cred_form form='form-to-edit-a-provider-select-subspecialty' post='[wpv-post-id]']
<script type="text/javascript">
jQuery(document).ready(function () {
    jQuery('input[type="radio"][name="select-subspecialty"][value="[wpv-post-id item="@subspecialty-provider.parent"]"]').attr('checked', 'checked');
});
</script>
[/wpv-conditional]
[wpv-conditional if="( '[wpv-post-id item='@subspecialty-provider.parent']' ne '' )"]
<h2 style="padding-top:40px;">Conditions and Treatments</h2>
[cred_form form='form-to-edit-a-provider-select-condition-treatment' post='[wpv-post-id]']
<script type="text/javascript">
jQuery(document).ready(function () {
    jQuery('input[type="checkbox"][name="select-condition"][value="[wpv-post-id item="@subspecialty-provider.parent"]"]').attr('checked', 'checked');
});
</script>
[/wpv-conditional]

Thank you again and take care!

#1465453

Toolset Support doesn't debug Custom Code, generally.
Above Custom Code is mainly acting on cred_save_data(), a Toolset Forms API, so we can partially help with it, but technically, everything within those hooks is subject to custom code.

I also see you work with toolset_disconnect_posts and toolset_connect_posts which are as well Toolset APIs, and it seems it's those that produce the Fatal error.
It mentions, in the error, that it cannot find any valid Parent Post Value in your code.

This seems to happen in the Hook added with:
[/php]add_action('cred_save_data', 'condition_processing_action',10,2)[/php]

The error seems to happen at the las line of the function where you connect posts with toolset_connect_posts

That API is documented here:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_connect_posts

It expects:
- a slug of the relationship or an array of parent/child. You seem to use the slug of the relationship, so that should be fine if your relationship slug is "condition-provider"
- a valid parent post id or object. Here you use $_POST['select-condition']
- a valid child post id or object. Here you use $post_id

Now, the error mentions the parent, which is the first argument, so $_POST['select-condition'] is not returning a Post ID or Object like expected.

Can you check what the value of $_POST['select-condition'] in your code is?
You can use var_dump($_POST['select-condition']), error_log(print_r($_POST['select-condition'], true)); or other methods to check what the value is.

The only way the error could be thrown is if above $_POST['select-condition'] will not be a Post ID or object.
There are many reasons why this might fail, starting from how the field is populated (it uses nested ShortCodes with double apostrophes, which will break usually)
Once you know what $_POST['select-condition'] returns in the code, you can start debugging the Form, where the field is populated (I see that's done with a conditional and then depending on other selected fields, output the assumed parent of the post, which is again not always working on Forms, as that post might not be related to the current post at all, or not yet exist, or not yet be connected)

Unfortunately, until we do not know what $_POST['select-condition'] holds, we cannot determine what is breaking it precisely.
A first quick test could be to use a hardcoded value in the field select-condition of an existing Post (an ID) so that you can check if the issue is the Custom Code or the form itself and how it populates the Field.
I suspect the latter.

Please let me know so we can then proceed to check upon the form
Likely, a staging instance will be required for this.

#1469531
relationship-slug.png
form-slug.png

Hello Beda,

Thank you for your reply!

We did not know we were using custom code. This super helpful guy Waqar was walking us through all of this and we've just been following along (https://toolset.com/forums/topic/adding-several-custom-fields/). Our setup is almost complete and if we can just get a little help to push us over the finish line, that would be greatly appreciated.

I double checked our setup and the form checkbox field slug that we use to collect the conditions is named 'select-condition' and our relationship slug is named 'condition-provider'; which matches the custom code.

We have a staging environment and administrative login ready for you if needed.

Thank you again for everything! Take care.

#1470641

It is custom code in as far it is not something supported within the native Toolset, which is why custom code is needed - even if it uses the API of Toolset 🙂

Anyway, let me see how we can solve this.

In the first snippet, please modifiy the code like this:

if(!empty($_POST['select-condition'])) {
                // connect the selected condition with the provider
               // toolset_connect_posts( 'condition-provider', $_POST['select-condition'], $post_id );
               error_log(print_r($_POST['select-condition'], true));
               error_log(print_r($post_id, true));
            }  

We just told PHP to log the variables $_POST['select-condition'] and $post_id.
This will help us to see what they hold.

Now in wp-config.php please enable debugging and logging like so:

define( 'WP_DEBUG', true );//Turn on Debug for WP
define( 'WP_DEBUG_LOG', true );//Log the debug for WP
ini_set( 'log_errors', true );//Tell PHP to log errors
ini_set( 'error_reporting', E_ALL );

Now, fire the form again - it should now log to the error log the values of those variables
These should correspond to the parent post and child post.

It is very possible that those will be either wrong, or empty.
I suggest, in this case, to fire your code later at cred_submit_complete():
https://toolset.com/documentation/programmer-reference/cred-api/#cred_submit_complete

This makes sure the post is already saved and only then connects it.
Your code would stay the same, just the hook changes when you add filter:

add_action('cred_submit_complete', 'subspecialty_processing_action',10,2);
function subspecialty_processing_action($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==2211)
    {
        // delete the existing connection between provider and subspecialty
            // get existing connection
            $existing_connections = toolset_get_related_posts($post_id,'subspecialty-provider','child');
            if(!empty($existing_connections)) {
                // delete the found connection
                toolset_disconnect_posts( 'subspecialty-provider', $existing_connections[0], $post_id );      
            }
  
        // if any option for a subspecialty is selected connect it to the provider
            // if any option for a subspecialty is selected
            if(!empty($_POST['select-subspecialty'])) {
                // connect the selected subspecialty with the provider
                toolset_connect_posts( 'subspecialty-provider', $_POST['select-subspecialty'], $post_id );
            }  
    }
}

Please let me know if this helps to resolve the issue, if not, please let me know what you see in the debug log as value of those variables

#1471883

Hello Beda,

Oh, we didn’t know that was custom code. We were just following along with support. Thank you so much for that! We don’t know PHP so Toolset support has been a godsend.

I think we’re almost there, but we did have an issue which is none of the subspecialty, condition, and treatment forms work now that we put the code snippet in…and there’s no troubleshooting error messages either (even after adding the code to the wp-config.php file).

Any help with this would be greatly appreciated. Thank you again!

#1472569

I am afraid that without any coding knowledge at all, using the Toolset/WordPress API will be tricky and is not recommended, because to use WordPress and Toolset API you need a minimal PHP knowledge.

I am not sure why ALL forms would stop working due to the code I posted above, it addresses one only form:
ID 2211.
It, however, uses the same function name as your first code, hence, that can cause issues, you should either delete the first code or rename the function.

However before you even try the complete snippet I provided as example you would have to debug your current code, which is why I suggest to add debug constant to wp-config.php and then dump the variables in your code.
For this, you need to modify your current code as suggested in https://toolset.com/forums/topic/custom-post-type-form/#post-1470641

This means your code that now is:

<?php
/**
 * Process condition relationships with providers
 */
 
toolset_snippet_security_check() or die( 'Direct access is not allowed' );
 
// Put the code of your snippet below this comment.
// function to process condition relationships with providers
add_action('cred_save_data', 'condition_processing_action',10,2);
function condition_processing_action($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==2217)
    {
        // delete the existing connection between provider and condition
            // get existing connection
            $existing_connections = toolset_get_related_posts($post_id,'condition-provider','child');
            if(!empty($existing_connections)) {
                // delete the found connection
                toolset_disconnect_posts( 'condition-provider', $existing_connections[0], $post_id );      
            }
  
        // if any option for a condition is selected connect it to the provider
            // if any option for a condition is selected
            if(!empty($_POST['select-condition'])) {
                // connect the selected condition with the provider
                toolset_connect_posts( 'condition-provider', $_POST['select-condition'], $post_id );
            }  
    }
}

becomes this:

<?php
/**
 * Process condition relationships with providers
 */
 
toolset_snippet_security_check() or die( 'Direct access is not allowed' );
 
// Put the code of your snippet below this comment.
// function to process condition relationships with providers
add_action('cred_save_data', 'condition_processing_action',10,2);
function condition_processing_action($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==2217)
    {
        // delete the existing connection between provider and condition
            // get existing connection
            $existing_connections = toolset_get_related_posts($post_id,'condition-provider','child');
            if(!empty($existing_connections)) {
                // delete the found connection
                toolset_disconnect_posts( 'condition-provider', $existing_connections[0], $post_id );      
            }
  
        // if any option for a condition is selected connect it to the provider
            // if any option for a condition is selected
            if(!empty($_POST['select-condition'])) {
                // connect the selected condition with the provider
                //toolset_connect_posts( 'condition-provider', $_POST['select-condition'], $post_id );
               error_log(print_r($_POST['select-condition'], true));
               error_log(print_r($post_id, true));
            }  
    }
}

If you now submit the form with ID 2217, you should get a log in the error log, if not, it means your $_POST['select-condition'] is empty.

Then you could try this code instead to confirm it:

<?php
/**
 * Process condition relationships with providers
 */
 
toolset_snippet_security_check() or die( 'Direct access is not allowed' );
 
// Put the code of your snippet below this comment.
// function to process condition relationships with providers
add_action('cred_save_data', 'condition_processing_action',10,2);
function condition_processing_action($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==2217)
    {
        // delete the existing connection between provider and condition
            // get existing connection
            $existing_connections = toolset_get_related_posts($post_id,'condition-provider','child');
            if(!empty($existing_connections)) {
                // delete the found connection
                toolset_disconnect_posts( 'condition-provider', $existing_connections[0], $post_id );      
            }
  
        // if any option for a condition is selected connect it to the provider
            // if any option for a condition is selected

                // connect the selected condition with the provider
                //toolset_connect_posts( 'condition-provider', $_POST['select-condition'], $post_id );
               error_log(print_r($_POST['select-condition'], true));
               error_log(print_r($post_id, true));

    }
}

That would then likely throw some log value - but empty.
This would then mean your form field "select-condition" has no value when you submit the form.

This would explain why you get the PHP error - which tells us there is no valid Post ID in that variable.
We need a valid ID in that variable to connect the posts.

Did this ever work before? If so, what was the action taken before it broke? Maybe you renamed a field or relationship?