Skip Navigation

[Resolved] Random number as permalink

This support ticket is created 5 years, 2 months ago. 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)

This topic contains 16 replies, has 3 voices.

Last updated by avansisI-2 5 years, 2 months ago.

Assisted by: Minesh.

Author
Posts
#1325891

Hi Toolset,

I would like to create an employee database where employees can submit an anonymous request. Visitors are therefore not allowed to deduce from the permalink what their name is.

Is there a way to give a random unique id or the post id as a permalink?

Thanks,
Menno

#1325901

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

I would like to know when you want to assign a unique ID (post-slug correct?) using Toolset form or while saving the post from admin?

#1325919

Hi Minesh,

I would like to have it on both 🙂 but from a Toolset Form is fine for now. I added %post_id% to the post type rewrite, but nothing happens when i add a post in the admin. Does this only work in a form?

Thanks,
Menno

#1325933

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

To update the post name with unique ID can you please try to add the following code to your current theme's functions.php
OR
You can add the following code to "Custom Code" section offered by Toolset:
=> https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/

For example:

function func_update_post_name( $post_id, $post ){
 
    if ( 'post-type-slug' == $post->post_type ) {
 
        remove_action( 'save_post', 'func_update_post_name',30,2 );
 
        // update the post slug
        wp_update_post( array(
            'ID' => $post_id,
            'post_name' => uniqid() 
        ));
 
       add_action( 'save_post', 'func_update_post_name', 30, 2 );
    }
}
add_action( 'save_post', 'func_update_post_name', 30, 2 );

Where:
- Replace post-type-slug with your original post type slug.

More info:
=> https://toolset.com/documentation/customizing-sites-using-php/updating-types-fields-using-php/

#1325939

Hi Minesh,

Thank you, this works like a charm!

hidden link

Great work!

Thanks,
Menno

#1325941

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Glad to know that solution I shared worked for you and help you to resolve your issue. Please feel free to resolve this ticket. 🙂

#1325943

Hi Minesh,

I have one more question, with your function the title refreshes on save. Is it possible to create a unique id only at the first publish and not on revisions (resaves)?

#1325947

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

What if you try to use the following code and add it to functions.php or "Custom Code" section offered by Toolset:

add_filter( 'get_sample_permalink', 'func_change_post_name_slug', 10, 5 );
function func_change_post_name_slug( $permalink, $post_id, $title, $name, $post ){
         
     if (defined('DOING_AJAX') && DOING_AJAX and $_REQUEST['action']=='sample-permalink') {
  
              if($post->post_type == 'post-type-slug' ) {
                   $permalink[1] = uniqid() ;
             }
      
    }
    return $permalink;
}

Where:
- Replace post-type-slug with your original post type slug.

#1325949

Do i need to add it or replace the previous function?

#1325953

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

You should remove the earlier save_hook code and add the solution I shared with filter get_sample_permalink.

#1325957
function func_update_post_name( $post_id, $post ){
  
    if ( 'employees' == $post->post_type ) {
  
        remove_action( 'save_post', 'func_update_post_name',30,2 );
  
        // update the post slug
        wp_update_post( array(
            'ID' => $post_id,
            'post_name' => uniqid() 
        ));
  
       add_action( 'save_post', 'func_update_post_name', 30, 2 );
    }
}
add_action( 'save_post', 'func_update_post_name', 30, 2 );


add_filter( 'get_sample_permalink', 'func_change_post_name_slug', 10, 5 );
function func_change_post_name_slug( $permalink, $post_id, $title, $name, $post ){
          
     if (defined('DOING_AJAX') && DOING_AJAX and $_REQUEST['action']=='sample-permalink') {
   
              if($post->post_type == 'employees' ) {
                   $permalink[1] = uniqid() ;
             }
       
    }
    return $permalink;
}

What do i need to change here?

Thanks,
Menno

#1325961

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you just add the following code and check:

add_filter( 'get_sample_permalink', 'func_change_post_name_slug', 10, 5 );
function func_change_post_name_slug( $permalink, $post_id, $title, $name, $post ){
           
     if (defined('DOING_AJAX') && DOING_AJAX and $_REQUEST['action']=='sample-permalink') {
    
              if($post->post_type == 'employees' ) {
                   $permalink[1] = uniqid() ;
             }
        
    }
    return $permalink;
}
#1326127

Hi Minesh,

This code creates a random id but after saving it goes back to the normal permalink.

#1327027

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Ok - the reason why it was not working because two actions fired. one action fired using AJAX automatically once you write your title and one action save_post is fired when post is published.

Can you please try to use the following code and remove all other codes I shared previously:

function func_update_unique_custom_slug( $data, $postarr ) {
 
	if($data['post_status']=='auto-draft' and $data['post_type']=='employees')
			$data['post_name'] = uniqid();

    return $data;
}
add_filter( 'wp_insert_post_data', 'func_update_unique_custom_slug', 99, 2 );

Can you please confirm it works as expected.

#1327785

Hi Minesh,

Yesss now it creates a unique id and keeps having this id when saving again!!

Great work 🙂

Thank you,
Menno