My overall goal:
I want to give users a warning if they create a post (via a Cred form) that has the same title as one that already exists.
The user does not enter the title directly: instead, I generate the title of the post using various fields (including relationships) within the form.
I therefore have to generate the post title in a cred_submit_complete action - because it's only then that all the information I need is conveniently available - and so the only place I can test for a duplicate title is in a cred_submit_complete action AFTER I've generated the new title.
If I find a duplicate title, I want to warn the user via a popup.
To test this out, I included this code in the cred_submit_complete action:
$msg = "Duplicate post title";
echo "<script type='text/javascript'>alert('$msg');</script>";
But instead of getting my alert I get an error alert instead:
lien caché
That code works in other contexts, but it seems not to work from within a cred_submit_complete action.
Is this what you would expect?
If so, is there a way round it? Or another way to achieve my overall goal?
Thanks
Alex
You won't be able to echo or inject a script like that in the submit complete hook. What about the validation API? I would use cred_form_validate instead and display an error message inline against one of the fields. You'll still have access to all the custom field values like you would have during cred_submit_complete, so you can test your assembled title there. The post won't be created until the validation API is passed successfully without returning errors inline.
https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate
Hi Christian
Thanks for confirming I can't use such scripts in the submit complete hook.
My first approach WAS to use the Validation API, but some titles use information from parent relationships, and even fields within parents of parents and I thought I wouldn't be able to get to that info easily from the Validation API.
And if I could, I'd have to replicate the title-creation code, I think.
I'm now thinking that a different approach might be best: making use of WordPress's wp_insert_post hook
Alex
even fields within parents of parents and I thought I wouldn't be able to get to that info easily from the Validation API.
You won't be able to get that information any more or less easily from the cred_submit_complete hook, unless I'm misunderstanding something. You'll still have to use the post relationships API to access ancestor post information.
I'm now thinking that a different approach might be best: making use of WordPress's wp_insert_post hook
Okay, that's kind of outside the scope of Toolset though. Let me know if you need assistance with Toolset APIs in your hook code.
OK, Christian.
If I were designing from scratch, I'd now probably do the title creation as part of the validation API, and check for duplicates immediately, so avoiding the actual creation of a duplicate post.
But given where I am now, my shortest route to fixing this is the wp_insert_post hook.
Thanks.
Issue resolved!