[Resuelto] Use username and date as post title and slug
This support ticket is created hace 8 años, 6 meses. 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.
No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.
I have a custom post type. I have a CRED form to add posts to the custom post type. I'd like to automatically insert the publish date and username as the post title and post slug.
Hello. Thank you for contacting the Toolset support.
You can use "cred_save_data" hook to generate automated post title.
For example:
add_action("cred_save_data", "my_save_data_action",10,2);
function my_save_data_action($post_id, $form_data)
{
global $current_user;
// if a specific form
if ($form_data["id"]==77)
{
$username = $current_user->user_nicename;
$date = get_the_date();
$title = $username."--".$date;
// Update the post into the database
$my_post = array(
'ID' => $post_id,
'post_title' => $title
);
wp_update_post( $my_post );
}
}
Where:
77 => Please replace "86" with your original form CRED ID.
Hope this helps and resolve your issue. Please let me know your feedback.
The title part works great! Can we also set the post slug at the same time? Currently it defaults to /auto-draft-(number). I'd like the slug to also use the username and date, kind of like /username-####-##-##
I'll look at your reference docs and see if I can figure it out. If I do I'll come back and mark as "resolved," but if not, let me know please.
I've added "post_name" column that sets the slug for the post. Please try following code:
add_action("cred_save_data", "my_save_data_action",10,2);
function my_save_data_action($post_id, $form_data)
{
global $current_user;
// if a specific form
if ($form_data["id"]==77)
{
$username = $current_user->user_nicename;
$date = get_the_date();
$title = $username."--".$date;
// Update the post into the database
$my_post = array(
'ID' => $post_id,
'post_title' => $title,
'post_name' => $title
);
wp_update_post( $my_post );
}
}
Where:
77 => Please replace "86" with your original form CRED ID.