Change permalink of custom post type
-
Hello, I am using jetengine and creating a custom post type, permalink inherits the “title” field. Is it possible to change the default permalink for the CPT with this plugin? Ideally randomize it with numbers? If yes, is it possible with the free version or pro?
Thanks
-
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-permastructuresTo 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? ThanksHi @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?
ThanksTo 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!
Hello, a follow-up question about this:
1) If I temporarily or permanently disable/delete the plugin, will the permalinks be set without any problem to the default? (default being the “Title” of the post instead of the numeric random string)
2) If I’d want to keep the plugin but reset the settings (for this cpt or any other cpt I change the permalinks structure in the future) to default or just set it the way it was before (add the title / meta field value) to the URL, will it automatically rewrite the existing URLs and then work on any new custom post I create?
If it rewrites, will the redirects work fine, or any problems I can expect?Thanks
Hi @dkanika,
After removing/deactivating the plugin, the functions responsible for overwriting the built-in permalinks will no longer be active, so the original URLs will be restored. Before that, if needed, you can remove all the settings and custom permalinks using “Debug” section.
All the changes to the permastructures will apply to new permalinks and by default will not affect existing URLs (until you manually reset them using “Regenerate/reset” tool).
As regards redirects, the free version redirects only built-in/native URLs. You can preview them inside the permalink editor:
https://permalinkmanager.pro/docs/basics/redirect-functions/#1-canonical-redirect
- You must be logged in to reply to this topic.