Generate Random 25 Character Permalink

Description/Instructions

This REQUIRES permalink manager (free). Grab a copy here.

This snippet allows you to add the random string to the permalink instead of the default wordpress one. This is good to make each URL unique, a little bit of privacy and hard to memorize for security reasons.

If this snippet helped, feel free to buy me a taco :)

function pm_random_string_field( $default_uri, $native_slug, $post, $slug, $native_uri ) {
// Do not affect native URIs
if ( $native_uri || empty( $post->post_type ) ) {
return $default_uri;
}

if ( false !== strpos( $default_uri, '%random%' ) ) {
$length = 25;
$random_string = wp_generate_password( $length, false );

$default_uri = str_replace( '%random%', $random_string, $default_uri );
}

return $default_uri;
}
add_filter( 'permalink_manager_filter_default_post_uri', 'pm_random_string_field', 3, 5 );

  • PHP
Copy Code

Let's Chat About this Snippet