I have a CRED form on our site that allows guest users (those who are not logged into an account) to submit a form that creates a post. Because I have no intention of letting these users have their own accounts on the site, I'd like to assign the post submission to a specific user, perhaps a dummy account that no real human uses, because otherwise it just assigns the post to the user that appears first alphabetically, and this is simply undesirable behavior.
Any help in this matter would be greatly appreciated. Thanks in advance!
Hi,
Thank you for contacting us and I'd be happy to assist.
You can use the 'cred_save_data' hook to execute a custom function that sets the specific (dummy) user as the post author when the form is submitted:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
For example, suppose that the form ID of your target form is '1234' and the dummy user that you'd like all guest posts to assign to has the ID '5678'. The custom function attached to the 'cred_save_data' hook will look like this:
add_action('cred_save_data', 'custom_set_guest_post_author',10,2);
function custom_set_guest_post_author($post_id, $form_data)
{
// check if a specific form
if ($form_data['id']==1234)
{
$args = array(
'ID' => $post_id,
'post_author' => 5678, // ID of the guest user
);
$res = wp_update_post( $args, true );
}
}
The above code snippet can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through the active theme's "functions.php" file.
I hope this helps and please let me know if you need further assistance.
regards,
Waqar