Skip Navigation

[Résolu] Use username and date as post title and slug

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.

This topic contains 4 réponses, has 2 voix.

Last updated by Eric Il y a 5 années et 10 mois.

Assigned support staff: Minesh.

Auteur
Publications
#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: Anglais (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: Anglais (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