Skip Navigation

[Resolved] Updating past relationship code

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to update to PHP 7 and upgrade to the new post relationships system. I have some custom code that may need to be updated.

Solution: Make sure the cred_save_data hooks are formatted with all 4 parameters when necessary. The other code looks okay as long as you do not re-generate Forms code.

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

This support ticket is created 6 years, 3 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 8 replies, has 2 voices.

Last updated by Arrien 6 years, 2 months ago.

Assisted by: Christian Cox.

Author
Posts
#1087568

Last year, I created a moneyless check-out system with the help of Toolset and it's technical staff.

I have been testing the site with PHP 7 and things seem to get a little buggy. Also, I am not sure how to update the custom code so that it works with the new post-relationship.

Here is the custom codes that was developed last year and I made them plugins

<?php
/*
Plugin Name: CUCCR Membership System / Add-on for WP Types
*/

//Save Member name as post title.
add_action('cred_save_data','func_custom_post_title', 10, 2);
function func_custom_post_title($post_id,$form_data) {
	$arr = array(18, 17); // here you can add more CRED form IDs
	if (in_array($form_data['id'], $arr)) {
		$type = get_post_meta($post_id, 'wpcf-member-type', true);
		$title = '';
		if ($type == 'individual') {
			$firstname = get_post_meta($post_id, 'wpcf-member-first-name', true);
			$lastname = get_post_meta($post_id, 'wpcf-member-last-name', true);
			$title = $firstname. ' ' . $lastname;
		}
		if ($type == 'group') {
			$groupname = get_post_meta($post_id, 'wpcf-member-group-name', true);
			$title = $groupname;
		}
		if($title){
			$slug = sanitize_title($title);
			$args = array(
				'ID' => $post_id,
				'post_title' => $title,
				'post_name' => $slug
			);
		wp_update_post($args);
		}
	}
}
<?php
/*
Plugin Name: CUCCR Member Stats / Add-on for WP Types
*/

//Calculate membership fields
function count_numeric_custom_field_func( $atts ) {
    global $wpdb;
    extract( shortcode_atts( array(
        'field' => '',
        'cpt' => '',
        'decimals' => 2,
        'format_decimals' => 2,
    ), $atts ) );
       
    $count = $wpdb->get_var( $wpdb->prepare(
        "
            SELECT SUM(CAST(meta_value AS DECIMAL (15,{$decimals}))) as count             
                FROM $wpdb->posts p 
                INNER JOIN $wpdb->postmeta pm          
        ON p.id = pm.post_id
        WHERE p.post_type = %s
        AND p.post_status = 'publish'
            AND meta_key = %s
        ",
    $cpt,
        $field
    ) );
    if (empty($count)){
        $count = 0;
    }
   
    return number_format($count, $format_decimals);
}

add_shortcode( 'count_numeric_custom_field', 'count_numeric_custom_field_func' );
<?php
/*
Plugin Name: CUCCR Check-in/Check-out System / Add-on for WP Types
Description: CUCCR Check-in/Check-out System
*/

//Save Check-in and Check-out Member name as post title.
add_action('cred_save_data','item_title');
function item_title($post_id, $form_data) {
            $type = get_post_type($post_id);
            if ($type == 'material-check-out')  {
                         
                        $parent_id = $_POST["_wpcf_belongs_member_id"];
                        $title = get_the_title($parent_id);
                        $slug = sanitize_title($title);
                        wp_update_post(array('ID' => $post_id, 'post_title' => $title, 'post_status'   => 'publish'));
            }
    
            if ($type == 'material-check-in')  {
                         
                        $parent_id = $_POST["_wpcf_belongs_member_id"];
                        $title = get_the_title($parent_id);
                        $slug = sanitize_title($title);
                        wp_update_post(array('ID' => $post_id, 'post_title' => $title, 'post_status'   => 'publish'));
            }
}

Where do I need to make changes?

Thank you

#1087683

CUCCR Membership System
No modifications are needed here.

CUCCR Member Stats
No modifications are needed here.

CUCCR Check-in/Check-out System
The only relationship code I see here are the two lines that give you access to the parent post ID from the Form fields:

$parent_id = $_POST["_wpcf_belongs_member_id"];

These two lines may or may not need to change. It depends on whether or not you modify the Form code after migrating onto the new relationships system. If you do not modify the Form code you already have in place, no changes are necessary. However, if you re-generate the Form code after migration, a new name will be applied to this field and you must adjust your code to match the new name. The new name will be based on the new post relationship slug.

#1088389

Thanks Christian,

That all makes sense.

However, when I am running the site locally using PHP 7 and I try to use the form related to CUCCR Check-in/Check-out System, I get an error when submitting the form. Is it a PHP 7 error or something else?

Fatal error: Uncaught ArgumentCountError: Too few arguments to function item_title(), 1 passed in C:\xampp\htdocs\cuccr\wp-includes\class-wp-hook.php on line 288 and exactly 2 expected in C:\xampp\htdocs\cuccr\wp-content\plugins\check-in_check-out.php\check-in_check-out.php:9 Stack trace: #0 C:\xampp\htdocs\cuccr\wp-includes\class-wp-hook.php(288): item_title(9137) #1 C:\xampp\htdocs\cuccr\wp-includes\class-wp-hook.php(310): WP_Hook->apply_filters('', Array) #2 C:\xampp\htdocs\cuccr\wp-includes\plugin.php(453): WP_Hook->do_action(Array) #3 C:\xampp\htdocs\cuccr\wp-content\plugins\cred-frontend-editor\application\models\form\post.php(625): do_action('cred_save_data', 9137, Array) #4 C:\xampp\htdocs\cuccr\wp-content\plugins\cred-frontend-editor\application\models\form\base.php(399): CRED_Form_Post->save_form(9137) #5 C:\xampp\htdocs\cuccr\wp-content\plugins\cred-frontend-editor\application\controllers\form_builder_base.php(43): CRED_Form_Base->print_form() #6 C:\xampp\htdocs\cuccr\wp-content\plugins\cred-frontend-editor\app in C:\xampp\htdocs\cuccr\wp-content\plugins\check-in_check-out.php\check-in_check-out.php on line 9

I just want to make sure that when I switch over to PHP 7 on my live site that nothing breaks.

Thanks.

#1088483

I see now, your cred_save_data hook registration code is missing the last two parameters:

add_action('cred_save_data','item_title');

Add them like this:

add_action('cred_save_data','item_title', 10, 2);

Then let me know if the Fatal Error is not resolved.

#1090218

Thanks Christian,

However, now my posts now save with the title "Check-in Material" and not the parent id.

Thoughts?

#1090249

I will have to make a clone of your site to run some tests locally where I can monitor code execution to determine why this is happening. I can install the Duplicator plugin to begin creating that clone if it's okay with you. Let me know if you approve and I'll get started.

#1091249

Hi Christian,

Yes, I approve the Duplicating of the site for debugging purposes.

Thanks.

#1092326
Screen Shot 2018-08-26 at 11.13.50 AM.png
Screen Shot 2018-08-26 at 11.14.10 AM.png
Screen Shot 2018-08-26 at 11.12.52 AM.png
Screen Shot 2018-08-26 at 11.13.08 AM.png

I must be confused because when I submit the Check-in and Check-out Forms on my local clone, the new posts are created using the selected parent post titles, not IDs. Based on the code in the check-in / check-out plugin, that seems to be the desired outcome. See the attachments here, showing new Check-in and Check-out posts being created and the resulting new post titles. What have I misunderstood?

#1093941

Hi Christian,

You're not confused. I looked back at my local install and see it's a bit buggy. I will duplicate the same site you did and try again.

Thanks.