Skip Navigation

[Resolved] Use username and date as post title and slug

This support ticket is created 8 years, 5 months ago. 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.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 4 replies, has 2 voices.

Last updated by Eric 8 years, 5 months ago.

Assisted by: Minesh.

Author
Posts
#341976

Hi,

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.

Thanks,
Eric

#342047

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

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.

More reference:
=> http://codex.wordpress.org/Function_Reference/wp_update_post
=> https://toolset.com/documentation/user-guides/cred-api/

#342103

Thank you Minesh!

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.

Thanks again,
Eric

#342395

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

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.

#342601

That's perfect! Thank you so much!
Eric

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.