Skip Navigation

[Résolu] Is there a way to trim the post title to a maximum no. of characters in CRED

This support ticket is created Il y a 3 années et 3 mois. 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)

Ce sujet contient 5 réponses, a 2 voix.

Dernière mise à jour par Minesh Il y a 3 années et 3 mois.

Assisté par: Minesh.

Auteur
Publications
#2147181

When we create custom posts, sometimes the creator needs to give the post a really long title.
That's fine, but the URL (which gets generated from the post title) is uncomfortably long.

Ideally, is there a way, either through ToolSet natively or through some custom code, to limit the number of characters in the URL?
I don't care too much if words get cut off.
Alternatively, is there a way to construct the URL from the post ID?

#2147287

Minesh
Supporter

Les langues: Anglais (English )

Fuseau horaire: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

As I understand - you want to adjust the post slug when you save the post but I would like to know that when you want to adjust the post slug - while using Toolset Forms or when you create a new post from admin?

#2147337

Hello. I am talking about when a post is created using CRED.
Users will only be creating posts from the front end.

I want to trim the slug length so it isn't ridiculously long.

#2147343

Minesh
Supporter

Les langues: Anglais (English )

Fuseau horaire: Asia/Kolkata (GMT+05:30)

Ok - as you are using Toolset forms to create the post from frontend there is a way to trim the post slug.

You can use the Toolset Form's hook: cred_save_data
=> https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

For example:
You can add the following code to "'Custom Code" section offered by Toolset:
=> https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/#adding-custom-php-code-using-toolset

add_action('cred_save_data', 'func_adjust_post_slug', 10, 2);
function func_adjust_post_slug($post_id, $form_data) {
 
if ($form_data['id']==9999) {

$slug = md5( time() );
wp_update_post(array('ID'=>$post_id, 'post_name' => $slug));

}
}

Where:
- You can replace 9999 with your original form ID.
- you can adjust the code as required as currently it will generate the post slug based on time and md5() function.

#2147481

OK. Thanks.
But instead of:

$slug = md5( time() );

What I would prefer is:
a) take the user input title from the CRED form
b) trim it to a maximum number of characters
c) use that as $slug

I think the PHP code for that would be pretty simple.
Can you advise

#2147507

Minesh
Supporter

Les langues: Anglais (English )

Fuseau horaire: Asia/Kolkata (GMT+05:30)

The thing is that post slug (name) we need to keep it unique.

So, trimming the slug will not help as lets say user create a post with post title "Test 1" and another user also create post with title "Test 1" in this case the post title will be identical and we will require to make the post title unique.

What I would prefer is:
a) take the user input title from the CRED form
==>
The post title field will be already available with the form.

b) trim it to a maximum number of characters
==>
How do you want to trim that, to what number?

Another possible solution could be:

add_action('cred_save_data', 'func_adjust_post_slug', 10, 2);
function func_adjust_post_slug($post_id, $form_data) {
  
if ($form_data['id']==9999) {
$post_obj =  get_post($post_id);
$slug = explode("-",$post_obj->post_name);
if(count($slug) > 3) {
              $slug =  join("-",array_slice($slug, 0, 3))."-". time();
}else {
$slug = $slug."-". time();
}
wp_update_post(array('ID'=>$post_id, 'post_name' => $slug));
 
}
}