5

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 5

13

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;
}
3

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.

13
  • 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 and return $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.
    – BoltClock
    Commented Dec 23, 2010 at 12:35
  • BTW, That 10 .. What does it do? Commented Dec 23, 2010 at 12:39
  • 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.
    – BoltClock
    Commented Dec 23, 2010 at 12:41
  • This error is occurring - Missing argument 2 for append_slug() Commented Dec 23, 2010 at 12:51
2

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);
0
   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!

5
  • Actually, I want it to generate random number and store the slug like that. Commented Dec 23, 2010 at 12:34
  • 1
    ah, 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 ;)
    – Hannes
    Commented Dec 23, 2010 at 12:44
  • @Bidhas don't mention it, would probably be possible to check if $postarr already has an ID - but I'm not a 100% on that thou
    – Hannes
    Commented Dec 23, 2010 at 12:56
  • Checking that. Will post if it does. Commented Dec 23, 2010 at 12:58
  • 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.
    – Jake
    Commented Nov 19, 2012 at 17:00
0

You should operate with wp_ajax_sample-permalink action and name_save_pre filter.

More examples here: https://wordpress.stackexchange.com/a/190314/42702

Not the answer you're looking for? Browse other questions tagged or ask your own question.