Hi @dkanika,
Yes, you can use the plugin’s “Permastructure” settings to rewrite also custom post types declared with JetEngine:
https://permalinkmanager.pro/docs/basics/bulk-edit-custom-permalinks/#how-to-use-permastructures
To replace the title/slug with random string you will need to use an extra code snippet:
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 = 11;
$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 );
add_filter( 'permalink_manager_do_not_append_slug' ,__return_true' );This would allow to use a new tag: %random% inside Permastructure settings.
Hey, thanks for the prompt response. I installed the plugin and added the code, changed the permalink:
https://prnt.sc/wck9-zdCjOQ_
I created several new CPTs to test, but the random number doesn’t appear, the URLs are just
.com/cpt-category/
https://prnt.sc/6bpEZoOl6Gj1
And that’s it, the random string is not being added.
Any thoughts? Thanks
Hi @dkanika,
How was the code specifically added? Did you use a child theme or an additional plugin?
I accidentally made a typo in the last line of code, so if it was executed, it should result in a fatal PHP error. Just in case, here is the corrected version:
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 = 11;
$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 );
add_filter( 'permalink_manager_do_not_append_slug', '__return_true' );The snippet itself will work only for posts, pages and custom post type items that were published after the code was active. All URLs generated prior to inserting the code will be unaffected.
Hello,
I am using a code snippets plugin to add the code, it didn’t return a fatal error before. Now it works, however, it returns a random string of letters. I guess that works, but perhaps a little nicer would be a random string of numbers, is that possible?
Thanks
To use numbers in the random string, all you need to do is to replace:
$random_string = wp_generate_password( $length, false );with:
$random_string = rand( 100000, 999999 );
Wonderful, thanks a million for your help!
