When we add new post in wordpress, after supplying the post title, the slug is generated automatically. I need to edit that auto generation module so that i can add some arbitrary number in the end of the slug automatically. How to do it?
5 Answers
Don't use the hard-coded version that the OP used here. When he did that, there was not a filter available. More recently, since 3.3, a filter was added.
add_filter( 'wp_unique_post_slug', 'custom_unique_post_slug', 10, 4 );
function custom_unique_post_slug( $slug, $post_ID, $post_status, $post_type ) {
if ( $custom_post_type == $post_type ) {
$slug = md5( time() );
}
return $slug;
}
However this method will change the slug every time you save the post... Which was what I was hoping for...
EDIT:
This kind of works for limiting the generation to just once. The only drawback is that it creates one version when ajax runs after creating the title, then it creates another, permanent slug when the post is saved.
function custom_unique_post_slug( $slug, $post_ID, $post_status, $post_type ) {
if ( $custom_post_type == $post_type ) {
$post = get_post($post_ID);
if ( empty($post->post_name) || $slug != $post->post_name ) {
$slug = md5( time() );
}
}
return $slug;
}
Write a plugin to hook into the wp_insert_post_data
filter so you can update the slug before the post is sent for insertion into the database:
function append_slug($data) {
global $post_ID;
if (empty($data['post_name'])) {
$data['post_name'] = sanitize_title($data['post_title'], $post_ID);
$data['post_name'] .= '-' . generate_arbitrary_number_here();
}
return $data;
}
add_filter('wp_insert_post_data', 'append_slug', 10);
Note that this function requires that you allow WordPress to auto-generate the slug first, meaning you must not enter your own slug before generating, and it cannot update existing posts with the number.
-
Ok. can you please explain the flow of control here? I'm adding a filter, means when ever the function
wp_insert_post_data
is called, it must pass through the function i defined i.e.append_slug
, right? what does that 3rd parameter of add_filter (i.e.10
) do? Commented Dec 23, 2010 at 12:33 -
2@Bibhas: After WordPress is done generating the post slug, it immediately calls all functions that apply to the
wp_insert_post_data
filter (it's not actually a PHP function itself). WordPress passes$data
and$postarr
based on the post being updated so you can access$data['post_name']
(the slug). After you modify the slug andreturn $data
, WordPress will have the updated post data, ready to be inserted or updated in the DB. Just make this a plugin, activate it and WordPress will handle the rest automatically. I believe this filter is called every time you save a post. Commented Dec 23, 2010 at 12:35 -
-
10 is just an arbitrary value for telling WordPress when to call this filter function if there's anything else hooking onto that function. It shouldn't matter that much in your case. Commented Dec 23, 2010 at 12:41
-
This error is occurring -
Missing argument 2 for append_slug()
Commented Dec 23, 2010 at 12:51
Test this : (paste it into functions.php)
function append_slug($data) {
global $post_ID;
if (!empty($data['post_name']) && $data['post_status'] == "publish" && $data['post_type'] == "post") {
if( !is_numeric(substr($data['post_name'], -4)) ) {
$random = rand(1111,9999);
$data['post_name'] = sanitize_title($data['post_title'], $post_ID);
$data['post_name'] .= '-' . $random;
}
}
return $data; } add_filter('wp_insert_post_data', 'append_slug', 10);
add_filter('post_link','postLinkFilter', 10, 3);
/**
* Manipulates the permalink
*
* @param string $permalink
* @param stdClass $post
* @return string
*/
function postLinkFilter($permalink,stdClass $post){
return $permalink.'?12345';
}
Untested in that scenario, but I have already used it, should work with a minimum of changes, but try and test it REALLY Carefully .
In any Case, don't use rand()
here or something alike, since the function must return the same link for the same post every time, otherwise you will have some serious problems.
Have fun!
-
Actually, I want it to generate random number and store the slug like that. Commented Dec 23, 2010 at 12:34
-
1ah, so you want to manipulate the post-name of the post, not directly the slug creation process, in that case go with @BoltClock, but make sure to check if thats an insert or an update, otherwise your would append another random number each time you update an article - and I'm sure you don't wanna do that ;)– HannesCommented Dec 23, 2010 at 12:44
-
@Bidhas don't mention it, would probably be possible to check if
$postarr
already has anID
- but I'm not a 100% on that thou– HannesCommented Dec 23, 2010 at 12:56 -
-
post_link doesn't update the actual slug, it just updates the link returned for get_permalink() type functions. It is somewhat superficial. Also, there is page_link and post_type_link for different applications.– JakeCommented Nov 19, 2012 at 17:00
You should operate with wp_ajax_sample-permalink
action and name_save_pre
filter.
More examples here: https://wordpress.stackexchange.com/a/190314/42702