mbis

Use random string in permastructure

Aug 31st, 2020 (edited)
2,548
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Random string in permastructure
  3.  * Use %random% in "Permastructure" settings
  4.  */
  5. function pm_random_string_field($default_uri, $native_slug, $post, $slug, $native_uri) {
  6.     // Do not affect native URIs
  7.     if($native_uri == true || empty($post->post_type)) { return $default_uri; }
  8.    
  9.     if(strpos($default_uri, '%random%') !== false) {
  10.         $chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  11.         $length = 12;
  12.         $random_string = substr(str_shuffle($chars), 0, $length);
  13.        
  14.         $default_uri = str_replace('%random%', $random_string, $default_uri);
  15.     }
  16.    
  17.     return $default_uri;
  18. }
  19. add_filter('permalink_manager_filter_default_post_uri', 'pm_random_string_field', 3, 5);
Add Comment
Please, Sign In to add comment