Update entry slug

By: Greg Berthelot | Asked: 07/04/2022
ForumsCategory: General questionsUpdate entry slug
Greg Berthelot asked 2 years ago

Hi
I have a form which creates post entries. Slug is generated from fields eg. [title]-[date]-[unique-field]. Entries can be updated from frontend and I need to update the entry slug according to [date] field.
How can I achieve this?
Any help would be appreciated
Greg

 

Question Tags:
3 Answers
Rob LeVineRob LeVine answered 2 years ago
I'm not sure of the order of things with FF and the post creation, but if the post creation is called prior to the  "after create entry" hook, you can use it to manually update the wp_posts table.


https://formidableforms.com/knowledgebase/frm_after_create_entry/
Victor Font answered 2 years ago

You can use the frm_new_post filter to formulate your slug. https://formidableforms.com/knowledgebase/frm_new_post/

Greg Berthelot replied 2 years ago

Something like below?

Greg Berthelot answered 2 years ago

Something like ?

add_filter( 'frm_new_post', 'change_my_post_slug', 10, 2 );
function change_my_post_slug( $post, $args ) {
if ( $args['form']->id == 25 ) { //change 25 to the ID of your form
$slug = $_POST['item_meta'][20] .'-'. $_POST['item_meta'][21];

//change each number (20, 21) to the ID of the field to use to build slug
$post['post_name'] = $slug;
}
return $post;
}
Victor Font replied 2 years ago

You're on the right track, but are you sure you want the Post's title to be the slug? The Create Post add-on has a dropdown setting to choose the source from which you want the slug to be generated. Most people leave it set to post title, which is fine for most purposes because this is the default WordPress behavior. When building a custom slug, I would take a different approach. I would build the custom slug and save it to a hidden field on the form. This provides the option to choose to construct the slug in jQuery or PHP. With jQuery you can build the slug in real time from the form's data and save it to the hidden field while the form is still in your user's browser. The hidden field can be selected as the slug source from the slug dropdown in the add-on.

Even when creating a CPT and populating the post title, I use a hidden field. This is because way back when I was first writing code to integrate Visual Views in CPT templates with the Genesis Framework, I discovered some quirkiness that occurs when you edit or display the linked post title field through the form or view. That little hiccup can disrupt custom solutions if you don't know about it. It has to do with where Formidable sources that field's data when editing. I discovered this "feature" a long time ago and I do not know if Formidable still does it this way or not. But when I first found it, Formidable displayed the value of the post's title field from the linked post, not the form field that linked to post title in the add-on.

I can understand why the developers might have chosen to do it this way. I just find it to be a somewhat odd system design choice that raises this question,"Why did the Formidable developers choose a post field as the source of truth instead of the original content as stored in Formidable?" I believe Formidable should be the only source of truth for any data captured through Formidable. It is the source system. It's befuddling.

If you're interested in learning more about using Visual Views in CPT templates, here's an article that explores the topic in depth. https://formidable-masterminds.com/formidable-visual-views-and-cpt-templates/.

As an FYI, here are two ways we use this capability with the Formidable Masterminds Developers Directory. Every developer that signs up in the Masterminds Developers Directory gets their own CPT that displays their contact details and link to their Developer Digital Store if they've opted to open one (future). The title is their directory display name. That is the only form data that is passed to the CPT. The slug is created from the title. The CPT template passes post id to a Formidable view that uses it to display the entry. Every tutorial on Formidable Masterminds also displays the author's contact details and content attribution through a standard post template that displays directory data with Formidable Views.

I'm sure there are a lot more inventive ways to do this as well. Please share what you've accomplished when you have your working code complete.

Greg Berthelot replied 2 years ago

Thanks for taking time sharing those informations. Actually the project is job offers website and an external website is sucking the offers without using the WP API. I am currently waiting some informations on how they proceed. The client need is to update offers to make them pushed up on the external website. Yeah it is a bit weird process. I will check what fits the best according to my client needs and will be back here once done

Making the Best WordPress Plugin even better - Together

Take on bigger projects with confidence knowing you have access to an entire community of Formidable Experts and Professionals who have your back when the going gets tough. You got this!
Join the community
crossarrow-right