Skip Navigation

[Resolved] Add Action: Auto Assign post parent in CRED form not working

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

Our next available supporter will start replying to tickets in about 2.12 hours from now. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+01:00)

This topic contains 8 replies, has 2 voices.

Last updated by Nigel 7 years, 2 months ago.

Assisted by: Nigel.

Author
Posts
#484151

I'm attempting to do exactly what this post I located on the forum asked and had solved. I have not had the same success.

https://toolset.com/forums/topic/automatically-set-parent-upon-cred-from-submit/

Is there any reason why this code wouldn't be working in more recent versions of toolset?
I have followed the instructions in the post and I cant seem to get it working. It seems fairly straight forward. I see the post is 3 years old, so perhaps something has changed?

#484222

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Hi Jon

I just tested this and found it works, let's clarify how you are doing this.

Specifically, the intention with this code snippet is that you have a CRED form to create child posts. If you auto-generate the markup for the form it will—if you have set up the post relationships—automatically add the field for the parent post selector, which you can delete as it won't be used.

You then edit the Content Template for the parent post type and insert the shortcode for this child post form. Note, if you are using the CRED Forms button, you are not inserting a child post form link, you are inserting the shortcode for the child post itself, which will be rendered directly on the parent post.

If that matches your set up then you should be able to add the code—which I'll repeat below—and only need to edit the id of the CRED child post form and the slug of the parent post type.

add_action( 'cred_save_data', 'my_save_data_action', 10, 2 );
function my_save_data_action( $post_id, $form_data ){

	if ( $form_data['id'] == 243 ) { // EDIT

		$parent_id = url_to_postid( $_SERVER['REQUEST_URI'] );
		update_post_meta( $post_id, '_wpcf_belongs_parent-slug_id', $parent_id ); // EDIT
	}
}

If you still have problems let me know.

#484487

Hi Nigel,

Thanks for your reply...

I have gone over and over it and I'm confident that the code I've inserted in functions.php is tailored for my website:

add_action( 'cred_save_data', 'my_save_data_action', 10, 2 );
function my_save_data_action( $post_id, $form_data ){
 
    if ( $form_data['id'] == 1237) { // EDIT
 
        $parent_id = url_to_postid( $_SERVER['REQUEST_URI'] );
        update_post_meta( $post_id, '_wpcf_belongs_team-photo_id', $parent_id ); // EDIT
    }
}

It is still not working... any other ideas?

#484578

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

I have another ticket with the same issue and they are using the same code and it is working, so I'm not sure why yours is not, I can take a look if you like.

I think I should take a copy of your site for testing to examine the problem.

I will mark your next reply as private so that I can get log-in credentials from you—you may want to create a temporary admin user for me to use that you can later delete. And be sure to have a current backup of your site, even though I don't intend to make any other changes than testing this code.

Be sure to include WordPress and FTP credentials.

Can you also confirm the url of where I can see the problem.

#484931

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Hi Jon

Now that I see your site I see why it is not working.

You are using default permalinks, so the parent post slug is not part of the url itself but is added as a url parameter (e.g. ?team-photo=u18-b-gold).

If you are agnostic about how the permalinks look, then you might change them to post name, which I would say the vast majority of sites use.

If there is a reason not to, you would need to rewrite the line that gets the parent post id from the url to parse the url parameters.

#484937

Awesome Nigel,

I have changed permalinks to post name...
Does that automatically fix everything? Do permalinks update now?

Jon

#484938

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Yes, if you changed the permalink setting then that will take immediate effect for all content.

Which should mean that the code above works to auto-assign the parent.

If not, let me know and I'll take another look.

#484940

I tested it very quickly and it looks like it's working well! Thanks heaps.

Now that you have access to my site, are you able to help me with bootstrap features?
I was trying to add glyphicons to buttons and they just don't seem to show. I've enabled bootstrap and my theme indicates that it has bootstrap included... I'm stumped.

#484945

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Hi Jon

I can help you with that but you will need to post the issue in a new thread, our policy is to limit each ticket to a single question which makes it easier for others researching problems in the forums to locate issues and solutions.

You are welcome to assign the thread to me to handle.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.