Skip Navigation

[Resolved] CRED: How to add a post slug filed to the forms?

This support ticket is created 6 years, 11 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.

Our next available supporter will start replying to tickets in about 8.66 hours from now. Thank you for your understanding.

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 3 replies, has 2 voices.

Last updated by Minesh 6 years, 11 months ago.

Assisted by: Minesh.

Author
Posts
#601499

Hi,
I am creating a CRED form to add posts of a CPT called "Books".
The form works perfectly fine. except that I can't find a "post slug" field.
I need to be able to adjust some of my post slugs.
I tried the following codes, but none of them worked:

<div class="form-group">
		<label>[wpml-string context='cred-form-new books-15973' name='Slug']Slug[/wpml-string]</label>
		[cred_field field='post_name' post='book' value='' urlparam='' class='form-control' output='bootstrap']
	</div>
<div class="form-group">
		<label>[wpml-string context='cred-form-new books-15973' name='Slug']Slug[/wpml-string]</label>
		[cred_field field='post_slug' post='book' value='' urlparam='' class='form-control' output='bootstrap']
	</div>
<div class="form-group">
		<label>[wpml-string context='cred-form-new books-15973' name='Slug']Slug[/wpml-string]</label>
		[cred_field field='slug' post='book' value='' urlparam='' class='form-control' output='bootstrap']
	</div>

can you please help?

thanks

#601501

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Well - I would like to know you want to adjust post slugs while creating new posts your using CRED edit form while editing your post?

#601502

Hi,
yes I want to adjust the slug while creating a new post. this form is to create new posts, not to edit current posts.
thanks.

#601510

Minesh
Supporter

Languages: English (English )

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

Ok fine - thanks for clarification.

You need to use CRED hook cred_save_data in order to rename the slug for newly added post.

For example - you should adjust the below code as per your need to build your custom slug (post_name):

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"]==9999)
    {
    
                 $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:
- Replace 9999 with your original CRED form ID

Related ticket that may help you:
=> https://toolset.com/forums/topic/auto-generate-post-title-and-nameslug-by-combining-other-fields/#post-495877

More info:
=> https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data