For Example:


How could i go about doing this and is this even possible ? I tried Searching this online but no question answer specifies hows for the above change ?

  • Do you want to do this for all pages & posts or any specific custom post type or posts from specific category only? Mar 26 '16 at 13:25
  • All posts on my website no exceptions not that i could think of
    – CodeAllDay
    Mar 26 '16 at 13:46

Please see below. This will solve your problem for all future posts that you publish. I have tried this code. But to be on safer side, please backup your DB before you try or tray on staging.

function custom_unique_post_slug( $slug, $post_ID, $post_status, $post_type ) {

    $post = get_post($post_ID); 

    /* Set a flag to know if the post slug is already updated to required format */

    $is_slug_updated = get_post_meta( $post->ID, 'is_slug_updated' ); 

    /* define our two constant strings. You may change this to actual constants. */ 
    $first_const_str = "ConstantCustomString-";
    $second_const_str = "-ConstantCustomString";

    /* Check our flag and check post type is post and check if post is being published */
    if ( $post->post_type == 'post' && $post->post_status == 'draft' ) {        

        $slug = ucwords( str_replace("-", " ", $slug ) ) ;          
        $slug = str_replace( " ", "", $slug );    
        $slug = $first_const_str . $slug . $second_const_str;

        /* Update flag to to true */

        update_post_meta( $post->ID, 'is_slug_updated', TRUE);        

    /* here we are checking if post is already published then no need to update the slug */

    if( $post->post_type == 'post' && $post->post_status == 'publish' ) {

        $slug = $post->post_name;

    return $slug;

add_filter( 'wp_unique_post_slug', 'custom_unique_post_slug', 10, 4 );
  • hey thanks a lot for this piece of code but i tried directly changing the url structure using Settings->permalinks->custom Permalink and it worked just fine
    – CodeAllDay
    Mar 26 '16 at 17:51
  • Yes, "ConstantCustomString" can be prefixed and postfixed to slug from Settings > Permalinks, however I thought that having first letter capital for each word in the slug without spaces or hyphen (like PostTitle) is also important requirement for you. This was not achievable from Settings > Permalinks and hence I custom coded it. Well, glad to know that backend permalink setting is sufficient for you. Good luck :-) Mar 27 '16 at 5:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.