Customize the auto generation of Post Slug in WordPress

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?

Related posts

Leave a Reply

5 comments

  1. 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;
    }
    
  2. 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.

  3. 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);
    
  4.    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!