When a WP person creates a brand new Put up the default conduct is that the Title is used as the idea for the Slug which kinds a serious a part of the URL/Permalink. I’m altering this default conduct.
My code (pasted beneath in case it helps) removes some widespread phrases, and reduces Slug size to about 6 phrases.
The issue is that the WP UI for the Put up doesn’t persistently replace, and I would like it to.
It is a typical chain of occasions:
- Create new Put up. URL/Permalink shall be much like:
http://instance.com/?p=32565
- Enter a Title; The Small Canine Has Spoken Reality on Monday
- Click on “Save Draft”
Upon profitable save, there are often 1 of two outcomes.
- Both the URL now appears just like the default WP one:
http://instance.com/the-small-dog-has-spoken-truth-on-monday/
, or - It stays because the one utilizing the ID.
If I refresh (or depart and are available again to the Put up), the URL is http://instance.com/small-dog-spoken-truth-monday/
– which is what I would like it to be.
Is there one other occasion, or methodology, be in in PHP or Javascript, the place I can set off WP to replace the person interface so the Slug displays the actual (modified) one?
Outdoors of trial and error I’m not sure find out how to proceed.
perform slug_save_post_callback( $post_id, $put up, $replace ) {
if (!empty($post->post_name)) { return; }
if ($post->post_type != 'put up' || $post->post_status == 'auto-draft') { return; }
$slug = preg_replace("/[^A-Za-z0-9 ]/", '', $post->post_title);
$remove_list = array(
'a', 'an', 'and', 'are', 'as', 'at', 'be', 'however', 'by', 'can', 'cant', 'did', 'do',
'for', 'from', 'get', 'obtained', 'had', 'has', 'hasnt', 'have', 'heres', 'how', 'i', 'in', 'is', 'its', 'simply',
'okay', 'like', 'not', 'o', 'of', 'off', 'on', 'solely', 'or', 'okay', 'says', 'than', 'that', 'the', 'their', 'theyre', 'this', 'to', 'use',
'what', 'whats', 'when', 'the place', 'who', 'whom', 'why', 'will', 'with', 'wont', 'you'
);
foreach($remove_list as $phrase) {
$slug = preg_replace("/b$wordb/i", "", $slug);
}
$slug = explode("-", sanitize_title($slug));
$slug = implode("-", array_slice($slug, 0, 6));
if ($slug == $post->post_name) { return; }
remove_action( 'save_post', 'slug_save_post_callback', 10, 3 );
wp_update_post(array(
'ID' => $post_id,
'post_name' => $slug
));
add_action( 'save_post', 'slug_save_post_callback', 10, 3 );
}
add_action( 'save_post', 'slug_save_post_callback', 10, 3 );