Skip Navigation

[Resolved] Hook 'cred_save_data' seems not working

This support ticket is created 5 years, 4 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 16 replies, has 2 voices.

Last updated by Minesh 5 years, 4 months ago.

Assisted by: Minesh.

Author
Posts
#1426277
Immagine1.png

Hi, I am trying to build a custom title for the custom type Fascicoli (slug "fascicolo") when saving.
My intention is to compile a string with 3 fields:
Title of a related post (relationship: rivista-fascicolo)
Content of the custom field "anno-solare"
Content of the custom field "numero-fascicolo"

I built a plugin with this code but nothing happens:

//Create a dynamic post title by the CRED form.
add_action('cred_save_data','func_custom_post_title',10,2);
function func_custom_post_title($post_id,$form_data) {
    if ($form_data['id']==56) {
		$title ="";
		$rivista_id = toolset_get_related_post( $post_id, $relationship='rivista-fascicolo' );
		$rivista = get_the_title($rivista_id); 
        $anno_solare = get_post_meta($post_id, 'wpcf-anno-solare', true);
        $numero_fascicolo = get_post_meta($post_id, 'wpcf-numero-fascicolo', true);
        $title= $anno_solare . '-' . $numero_fascicolo;
        $args = array(
		'ID' => $post_id,
		'post_title' => $title,
		'post_name'  => sanitize_title( $title )
		);
        wp_update_post($args);
    }
}

In the past I built something similar using ACF hook 'acf/save_post' and was ok.
My only doubt is the form_data id. I got it from the Toolset custom field group that contains all the fields and that is linked to my custom type Fascicoli. Is this right?

Thanks in advance.

#1426289

Sorry this is the right code but nothing still happens:

//Create a dynamic post title by the CRED form.
add_action('cred_save_data','func_custom_post_title',10,2);
function func_custom_post_title($post_id,$form_data) {
    if ($form_data['id']==56) {
		$title ="";
		$rivista_id = toolset_get_related_post( $post_id, $relationship='rivista-fascicolo' );
		$rivista = get_the_title($rivista_id); 
        $anno_solare = get_post_meta($post_id, 'wpcf-anno-solare', true);
        $numero_fascicolo = get_post_meta($post_id, 'wpcf-numero-fascicolo', true);
        $title= $rivista . '-' . $anno_solare . '-' . $numero_fascicolo;
        $args = array(
		'ID' => $post_id,
		'post_title' => $title,
		'post_name'  => sanitize_title( $title )
		);
        wp_update_post($args);
    }
}

#1427185

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

The screenshot you shared belongs to custom field group not Toolset forms.

As I understand, you have a Toolset post form where you want to apply the "cred_save_data" hook to customize your title dynamically.

Actually, to locate your form ID, you should navigate to Toolset => Post Forms and grab the ID of your form and replace that within the code you shared. It should work.

If I misunderstood your requirement, please feel free to correct me.

#1427621

HI Minesh,
You're correct.
Now I understand the problem. I have a basic account and I don't see the Post Forms link. 🙂

#1427625

Minesh
Supporter

Languages: English (English )

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

Great - I hope this issue is resolved 🙂

#1427637

My issue is resolved now. Thank you!

#1427929

I've reopened the ticket beacuse I studied new Forms plugin but maybe it's useless for what I need (please correct me if I'm wrong).
At this point I don't know if the cred_save_data hook is the solution for me.
There's a way to save a custom post type title compiling a string with 3 custom fields (one is a relationship)?
I wish obtain this result while saving the post from the backend.
Thanks in advance!

#1431345

Minesh
Supporter

Languages: English (English )

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

"cred_save_data" hook meant to work with Toolset forms from the frontend.

If you want to build such customized title from backend - you need to use the standard save_post action hook.

Please check the following related ticket where I shared such a solution:
=> https://toolset.com/forums/topic/add-auto-generate-post-title-from-fields/#post-1213144

Please check the following related Doc:
=> https://toolset.com/documentation/customizing-sites-using-php/updating-types-fields-using-php/

#1441655

Hi Minesh,
I tried to build a plugin with save_post action for the backend and it works!! Thank You for the advice.

Now I have to customize the title with a form on the frontend too, so in this case cred_save_data would be the right choice.
I created a Post Form on a page for the insert module but it doesn't works: instead of writing the name of the post in the relationship, it writes the title of the page where the form is.

Using the same code for updating, it works!


//Create a dynamic post title by the CRED form.
add_action('cred_save_data','auto_titolo_fascicolo_frontend',10,2);
function auto_titolo_fascicolo_frontend($post_id,$form_data) {
    if ($form_data['id']==417) {
        $title ="";
        $rivista_id = toolset_get_related_post( $post_id, $relationship='wpcf-rivista-fascicolo' );
        $rivista = get_the_title($rivista_id); 
        $anno_solare = get_post_meta($post_id, 'wpcf-fascicolo-anno-solare', true);
        $numero_fascicolo = get_post_meta($post_id, 'wpcf-fascicolo-numero', true);
        $title= $rivista . '-' . $anno_solare . '-' . $numero_fascicolo;
        $args = array(
        'ID' => $post_id,
        'post_title' => $title,
        'post_name'  => sanitize_title( $title )
        );
		echo ('ID: '.$post_id.' post_title: '.$title.'post_name: '.$rivista); 
        wp_update_post($args);
    }
}
#1441679

Sorry i have verified now that it doesn't work even with the update. It seems like not recognizing the relationship.

#1441803

Minesh
Supporter

Languages: English (English )

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

Based on the code you shared, it looks like the toolset_get_related_post() function not configured correctly. You should pass there relationship slug.

More info:
=> https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_post

Can you please share the problem URL where you want to build the dynamic title as well as admin access details so I check check further.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#1442561

Minesh
Supporter

Languages: English (English )

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

I see you shared the following link and with the following page I see there is Toolet form added:
=> hidden link

As you are using the Toolset form, you need to use the Toolet Form's hook to build the post title.

I'm still confuse when you want to build the dynamic post title using the custom field "fascicolo-anno-solare" and "fascicolo-numero"

For instance - I'm on the following page and if I submit the post using the form available with the following lin:
=> hidden link

Now, consider the following values for each field as per the screenshot:
=> hidden link

What post title you would like to generate based on the input given with the above screenshot?

Also, I do not know why you created a plugin as Toolset offers the place where you can add your custom code:
=> https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/

#1442941

Hi Minesh,
considering the values of the screenshot I woud like this: The Bamba - 2011 - 2323232 (for insert and update)
I know that with Toolset is possible insert custom code in a specific place. It is my habit to create plugin because I usually work with ACF and Oxygen. Oxygen delete all "ordinary" templates and has no a functions.php file. At the moment I'm using WP Ocean and Elementor but is possible I'll use Oxygen also for this site. I have to decide.
This is my first experience with Toolset

#1444439

Hi Minesh, if you need further explanations about this, feel free to ask.
What do you mean with "As you are using the Toolset form, you need to use the Toolet Form's hook to build the post title"?
Is not cred_save_data a Toolset forms hook?

#1444583

Minesh
Supporter

Languages: English (English )

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

You should simply deactivate the plugin you have created as I moved the code to "Custom Code" section offered by Toolset.

I've added the following code to the "Custom Code" section offered by Toolset (it's a good practice to add Toolset related custom code to "Custom Code" section)
=> hidden link

add_action('cred_save_data','auto_titolo_fascicolo_frontend',30,2);
function auto_titolo_fascicolo_frontend($post_id,$form_data) {
    if ($form_data['id']==374) {
        $title ="";
        
       // $rivista_id = toolset_get_related_post( $post_id, $relationship='wpcf-rivista-fascicolo' );
        $rivista_id = $_POST['@rivista-fascicolo_parent'];
        $rivista = get_the_title($rivista_id); 
        $anno_solare = get_post_meta($post_id, 'wpcf-fascicolo-anno-solare', true);
        $numero_fascicolo = get_post_meta($post_id, 'wpcf-fascicolo-numero', true);
        $title= $rivista . '--' . $anno_solare . '--' . $numero_fascicolo;
        $args = array(
        'ID' => $post_id,
        'post_title' => $title,
        'post_name'  => sanitize_title( $title )
        );

        wp_update_post($args);
    }
}

I've added a couple of test posts which you can see here:
=> hidden link

You should also try to check: hidden link