Skip Navigation

[Resolved] Unable to update custom table with the cred_submit_complete hook

This support ticket is created 3 years, 10 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.

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

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 4 replies, has 2 voices.

Last updated by himanshuS 3 years, 9 months ago.

Assisted by: Minesh.

Author
Posts
#1919661

I want to insert entries into a custom table using the cred_submit_complete hook after I submit the post form.

Expected behavior:
1) Create an expert review post using CRED form
2) Add a json to the custom field for expert review. (Works perfectly). More reference on thread - https://toolset.com/forums/topic/create-form-fields-dynamically-based-on-term-count/page/2/
3) Once the post is saved, retrieve the value stored in the custom field to insert values in a custom table. I am doing that because I am running some calculations on the skills rating data stored in the JSON to compute skill points. Hence, I need to process the data and then save churned out numbers in a custom table for easy access.
4) The code has been debugged for all obvious syntax issues but I keep on getting the error - Unexpected output of the code snippet:

This happens even if I disable the 'insert into' code and just doesn't go away.

Are there any restrictions on using cred_submit_complete to insert data in custom tables? If not, then what does unexpected output mean even when there is no output from the code.

I can give you access to the admin areas for more information. I can share the code and other details over a private message if that works.

#1919703

If this helpful, I was able to create a custom table using toolset snippet:

Custom table:
<?php
/**
* New custom code snippet (replace this with snippet description).
*/

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

// Put the code of your snippet below this comment.
global $wpdb;
$charset_collate = $wpdb->get_charset_collate();
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );

//* Create the teams table
$table_name = $wpdb->prefix . 'skill_level_evaluations';
$sql = "CREATE TABLE $table_name (
evalution_id INTEGER NOT NULL AUTO_INCREMENT,
evaluation_post_type varchar(255) NOT NULL,
evalution_post_id INTEGER NOT NULL,
taxonomy_slug varchar(255) NOT NULL,
parent_term_slug varchar(255) NOT NULL,
term_slug varchar(255) NOT NULL,
skill_level_3_score INTEGER,
skill_level_4_score INTEGER,
skill_level_5_score INTEGER,
skill_level_6_score INTEGER,
requester_id bigint(20) NOT NULL,
created_at datetime NOT NULL,
PRIMARY KEY (evalution_id)
) $charset_collate;";
dbDelta( $sql );
?>

Now, I have another custom code snippet that insert the data but it seems like I can't push data when the hook is cred_submit_complete.
This is the code to insert data in the table:
$sql_1= "INSERT INTO wp_skill_level_evaluations (evalution_id, evaluation_post_type, evalution_post_id, taxonomy_slug, parent_term_slug, term_slug, skill_level_3_score, skill_level_4_score, skill_level_5_score, skill_level_6_score, requester_id, created_at) VALUES(null, 'expert-review', '$post_id', 'skill-category', '$parent_term_slug', '$term->slug', '$level_3_skill_points', '$level_4_skill_points', '$level_5_skill_points', '$level_6_skill_points', '$requester_id', '$date')";

$wpdb->query($sql_1);

Let me know if is possible using toolset custom code section. And if not, what are my options?

#1919843

So It seems like I have found the issue. It is related to decoding of JSON stored in a custom field. I am using get_post_meta( ) to extract the json but it is not decoding.

$expert_review_rating_data = array();
$data_json = get_post_meta( $post_id, 'wpcf-expert-review-rating' );
// $valid_json_data = html_entity_decode($data_json); // tried this ---> returns null
$valid_json_data = stripslashes($data_json); //--> tried this returns null
$valid_json_data = trim($data_json); //--> tried this returns null
$expert_review_rating_data = json_decode($valid_json_data, true); // also returns null

sources: https://stackoverflow.com/questions/6359325/wordpress-custom-post-meta-json-string-wont-decode-with-json-decode
2- https://stackoverflow.com/questions/7511821/how-to-convert-json-string-to-array
3 - https://stackoverflow.com/questions/24001410/php-json-decode-not-working

I am not sure what kind of encoding is happening in posting json in the custom field and extraction it.

The json data in the custom field looks just fine:

[{"term-slug":"market-analysis","performance-tier-slug":"tier-3-professional","criterion-slug":"data-collection","criterion-value":"3"}]

#1920067

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

When you use get_post_meta() without the third param true/false it will return the array rather the single value.

Can you please try to change the following line of code from:

$data_json = get_post_meta( $post_id, 'wpcf-expert-review-rating' );

To:

$data_json = get_post_meta( $post_id, 'wpcf-expert-review-rating', true);

More info:
=> https://developer.wordpress.org/reference/functions/get_post_meta/

#1924629

My issue is resolved now. Thank you!