Skip Navigation

[Resolved] Modify URL of posts (want to use a prefix)

This support ticket is created 4 years, 10 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 12 replies, has 2 voices.

Last updated by BenjaminH2340 4 years, 10 months ago.

Assisted by: Minesh.

Author
Posts
#1238547

Hey guys,

I've set up a custom post type (lets name it "cars").
If I now create the post "A5" the permalink structure is like this: domain.com/cars/a5

What I want is that for every post I create a prefix will be added to the post-name. While my post titel is "A5" the url should be like this: domain.com/cars/audi-a5

The prefix for every post at this custom post type should be "audi-"
How do I do that?

Is there any documentation that you are following?
No

Is there a similar example that we can see?
No, but I guess it's clear enough we don't need an example

Thanks in advance 🙂

Kind regards,
Ben

#1238687

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Well - to update the post name with prefix "audi-" can you please try to add the following code to your current theme's functions.ophp file and try to resolve your issue.

For example:

function func_update_post_name( $post_id, $post ){

    if ( 'cars' == $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' => "audi-".$post->post_name  
        ));

       add_action( 'save_post', 'func_update_post_name', 30, 2 );
    }
}
add_action( 'save_post', 'func_update_post_name', 30, 2 );

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

#1239037

Hey Minesh,

thank you for your feedback.
I tried the code snippet and it is editing the url parameter. But it's not working correctly.

Without these code the URL is:
domain.com/cars/a5

After putting the code into the functions.php it is:
domain.com/cars/audi-audi-/

Its also counting up. If I create another post its adding a number:
domain.com/cars/audi-audi-2/

#1239040

Minesh
Supporter

Languages: English (English )

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

something strange happening.

I've adjusted the code little bit so in the URL the word "audi-" should be added only one time.

I checked the following code and it works.

function func_update_post_name( $post_id, $post,$update ){
 
    if ( 'cars' == $post->post_type ) {
		
		
        remove_action( 'save_post', 'func_update_post_name',30,3 );
		if($update) {
			if(strpos($post->post_name, "audi-") === false){
				// update the post slug
					wp_update_post( array(
						'ID' => $post_id,
						'post_name' => "audi-".$post->post_name  
					));
			}
		}
 
       add_action( 'save_post', 'func_update_post_name', 30, 3 );
    }
}
add_action( 'save_post', 'func_update_post_name', 30, 3 );
#1239048

Now it is putting in the word "audi" into the url but not adding the title "A5"

The URL is this:
domain.com/cars/audi-/

Instead of:
domain.com/cars/audi-a5/

It is also counting up the posts to /audi-2/, /audi-3/ and so on.

After this I copied the code and tried it with another custom post type. After uploading and refreshing the site I get this fatal error:

Fatal error: Cannot redeclare func_update_post_name() (previously declared in .../functions.php:13) in .../functions.php on line 56

Line 56 is the last "}" of the second code snippet.
It's a fresh wordpress site. There are only installed the theme, the childtheme, yoast and toolset types and views. It's without custom code or real content

#1239054

Minesh
Supporter

Languages: English (English )

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

After this I copied the code and tried it with another custom post type. After uploading and refreshing the site I get this fatal error:
Fatal error: Cannot redeclare func_update_post_name() (previously declared in .../functions.php:13) in .../functions.php on line 56
==> Well, thats obvious as you need to change the function name.

Now it is putting in the word "audi" into the url but not adding the title "A5"
==> Well - have you tried to publish or update the post? Can you please follow the following steps:

- Click on Add New button to add new post
- with post title field add post title as A7
- click on publish button and check you see the URL as: domain.com/cars/audi-a7/

You need to either publish or update the post to see the end result.

#1239055

Ahhh... Okay.
I saved the post and published it later (that's how the posts will be created). That's why it doesn't work.

Is there maybe a way to edit the snippet in a way that saving the post before publishing (most of the posts will be really big and need to be checked from someone else before publishing it) is possible?

#1239085

Minesh
Supporter

Languages: English (English )

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

Can you please try the following code. I adjust the code so it will support multiple post types and you need to adjust the other post types slug.

function func_update_post_name( $post_id, $post,$update ){
 
      // add your addtional post types slug here
      $allowed_post_types = array("cars","post-type-slug-1","post-type-slug-2");   
 
    if ( in_array($post->post_type,$allowed_post_types) ) {
					
		remove_action( 'save_post', 'func_update_post_name',30,3 );
			
				// update the post slug
					wp_update_post( array(
						'ID' => $post_id,	
						'post_name' => "audi-".sanitize_title($post->post_title)
					));
			
			  add_action( 'save_post', 'func_update_post_name',30,3 );
    }
}
add_action( 'save_post', 'func_update_post_name', 30, 3 );

add_filter( 'get_sample_permalink', 'func_change_post_name_slug', 10, 5 );
function func_change_post_name_slug( $permalink, $post_id, $title, $name, $post ){
	
        // add your additional post types slug here
	$allowed_post_types = array("cars","post-type-slug-1","post-type-slug-2");   
	
	 if (defined('DOING_AJAX') && DOING_AJAX and $_REQUEST['action']=='sample-permalink') {

        	  if(in_array($post->post_type,$allowed_post_types)) {
			$permalink[1] = "audi-".sanitize_title($title);
			
	          }
	
	}
	return $permalink;
}

Where you should replace the post types slug.

#1239090

It's working fine I guess! 🙂
Also saving the post before publishing seems to work.

Thank you very much Minesh. Can't tell you how much this is helping me 🙂

Two last questions:
• If I need for a specific custom post type another prefix I just need to duplicate this functions and rename them?
• A littel later I want to translate the site (maybe with wmpl - I've ne ver build a multilingual site, so I don't know the technically behaviour) - every language need it's own prefix because some use words in german which will be needed in english, russian and so on. Will this be a problem wit this solution? 🙂

#1239091

Minesh
Supporter

Languages: English (English )

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

Thank you very much Minesh. Can't tell you how much this is helping me ?
==> Your welcome. 🙂 Glad to help 🙂 thanks for your feedback 🙂

• If I need for a specific custom post type another prefix I just need to duplicate this functions and rename them?
==> NO. rather creating new function. you should check with if .. else if condition.

For example:

function func_update_post_name( $post_id, $post,$update ){
      
                     
        remove_action( 'save_post', 'func_update_post_name',30,3 );
             if($post->post_type == 'cars' ) {
                // update the post slug
                    wp_update_post( array(
                        'ID' => $post_id,    
                        'post_name' => "audi-".sanitize_title($post->post_title)
                    ));
            }else if($post->post_type == 'student' ){
                    // update the post slug
                    wp_update_post( array(
                        'ID' => $post_id,    
                        'post_name' => "student-".sanitize_title($post->post_title)
                    ));
            }
             
              add_action( 'save_post', 'func_update_post_name',30,3 );
    
}
add_action( 'save_post', 'func_update_post_name', 30, 3 );
 
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 == 'cars' ) {
                   $permalink[1] = "audi-".sanitize_title($title);
             }else if($post->post_type == 'student'){
                       $permalink[1] = "student-".sanitize_title($title);
             }
     
    }
    return $permalink;
}

Where:
- Add as many if contions and replace your post type slug and prefix. As I shared another example for student post type.

• A littel later I want to translate the site (maybe with wmpl - I've ne ver build a multilingual site, so I don't know the technically behaviour) - every language need it's own prefix because some use words in german which will be needed in english, russian and so on. Will this be a problem wit this solution? ==> This is a different question. As per our support policy, we entertain only one question per ticket. This will help other users searching on the forum as well as help us to write correct resolution summery.

However - here is the link that may help you:
=> https://toolset.com/documentation/translating-sites-built-with-toolset/prepare-site-run-multilingual/
=> https://toolset.com/documentation/translating-sites-built-with-toolset/

If you have any question related to WPML, we have a different support forum where you can ask for help:
=> https://wpml.org/forums/forum/english-support/

#1239093

Minesh
Supporter

Languages: English (English )

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

Feel free to close the ticket 🙂 Happy weekend.

#1239831

Minesh
Supporter

Languages: English (English )

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

Would you mind to resolve this ticket?

#1240244

My issue is resolved now. Thank you!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.