Skip Navigation

[Resolved] Function that Christian helped me with 2 years ago

This support ticket is created 5 years, 11 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
- 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+00:00)

This topic contains 6 replies, has 2 voices.

Last updated by Steve 5 years, 11 months ago.

Assisted by: Nigel.

Author
Posts
#1189214

Christian Cox helped me with this function below almost 2 years ago.

Im using it now with a different cred form to rename the post created from the first and last name in the submitted data.

The form writes all the data to the post correctly however if i goto to view it. I get a 404 Ive refreshed permalinks- When i remove the function the form saves as a draft with a random name and that views just fine. HAs something changed in the last 2 years that would cause this or might it be something else. Not sure what to do to trouble shoot

function sf_save_data_action_juried($post_id, $form_data){
// Change your CRED Form "ID" accordingly below
if ($form_data['id']==15806){

//Declare the content of your variables, change "your_custom_field_slug" accordingly
$first = get_post_meta( $post_id, 'wpcf-ca-first-name', true );
$last = get_post_meta( $post_id, 'wpcf-ca-last-name', true );
$custom_title = $last.' '.$first;

//collect data and define new title
$my_post = array(
'ID' => $post_id,
'post_title' => $custom_title,
'post_name' => $custom_title,

);

// Update the post into the database
wp_update_post( $my_post );

}
}
add_action('cred_save_data', 'sf_save_data_action_juried',10,2);
// use this hook if saving from back end save_post()

#1189540

Nigel
Supporter

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

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

Hi Steve

The one obvious flaw in that function is the post name (which is the slug used to create the permalink for the post) is the same string as the title (e.g. "John Smith" when it should be "john-smith"). If you've changed the permalink structure on your site that could be why it is not working.

You should use sanitize_title to create the post_name, so update this one line:

'post_name' => sanitize_title( $custom_title )

See https://developer.wordpress.org/reference/functions/sanitize_title/

Try updating that and see if it works.

#1189829

Thanks Nigel . I think Christian and i discovered at the time that wordpress fixed the slug on its own. But in any case when i sanitize it I get the same issue - the post title is not updated and now instead of getting a 404 I get a blank page when the form is submitted

Steve

#1189860

Nigel
Supporter

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

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

Do you see the post in the backend that was created with the form? And it is only when trying to visit it on the front-end that it has a problem?

It might help if I could take a look at your site.

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 changes.

Can you give me examples of posts submitted with the form that have this problem?

#1189898

Nigel
Supporter

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

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

Steve

I added the ARI Adminer plugin which gives me a phpMyAdmin-like interface to inspect the database, and looking at the entry for the test post (brettler steve) in wp_posts it appears entirely normal.

Everything looks okay on the post edit screen on the back-end, too.

So I don't think the problem is necessarily with the code snippet, because the generated post appears to be correct when viewed in the database.

I tried re-saving the post from the post edit screen, which didn't help, and reinforces the impression that there is something else at work here.

This looks like a live site, so I suggest you do some testing on a staging server, and as a first step try disabling all plugins except Types and switching theme to twentynineteen and seeing if you can visit that same post on the front end or not.

Depending on what you find we can see how to proceed.

#1189990

Nigel thanks and Im sorry for not doing this basic trouble shooting step first. Turns out Yoast SEO has created a redirect. I think likely i must have created a record using my name several years ago when I first made this form- Then deleted the record and Yoast made the redirect which has been in force since. I'll close the ticket.

Help much appreciated

#1189991

My issue is resolved now. Thank you!