I am using this function to alter/validate submitted post values.
This piece of code works as intended, so that’s not the issue but it’s added to acf/pre_save and I get the feeling it might be better of in acf/save post, so I’m looking for suggestions/opinions.
Some explanation about the code.
1. It first retrieves a title (which is an obligated field, so it’s always entered) then sanitises (to be used as a post name/slug) and then updates the title field.
2. It then compares the submitted profile photo to the stored photo. If a new photo is uploaded, a meta field is added to mark the photo ‘pending’.
3. It then links the uploaded image to the ad that is created (because the upload happens on a page with a front-end form and this way I’m sure it will be connected to the new post instead of the page the form is on).
4. It then updates post_title, post_status and post_name.
5. It then creates an array for any photos already uploaded before and compares the uploaded photos to the stored ones. It then links each photo to the correct post.
6. An admin mail is sent when a new profile pic is uploaded.
7. Cache is cleaned.
8. Trigger al transitions to make sure the post status is correctly changed.
1. is this piece of code better of in acf/save ?
2. would it be better to cut this piece of code up in smaller parts ?
3. would you handle it any differently ?
1) If it’s working as a pre_save_post filter, then I would leave it there
2) Will you use the same code in other places if you break it up? The only purpose of creating separate functions is if you’re going to use those function from other places. Don’t prematurely optimize your code if you’re not sure you’re going to use that optimization.
3) I would handle it in whatever way worked and made sense to me at the time I was writing it. I would also give thought to if I will understand the code when I look at it again in 6 months or a year.