• It is possible to remove a variable slug in a permalink generated by an external plugin?

    I am not talking about standard page, post, categories etc but dynamic link generated by user’s request.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Hello,
    I appreciate your question, well here is code for that

    function custom_post_type_permalink($permalink, $post, $leavename) {
        // Modify the permalink as needed
        $permalink = str_replace('your_variable_slug/', '', $permalink);
    
        return $permalink;
    }
    
    add_filter('post_type_link', 'custom_post_type_permalink', 10, 3);

    function custom_post_type_permalink($permalink, $post, $leavename) { // Modify the permalink as needed $permalink = str_replace(‘your_variable_slug/’, ”, $permalink); return $permalink; } add_filter(‘post_type_link’, ‘custom_post_type_permalink’, 10, 3);

    Thread Starter mobius01001

    (@mobius01001)

    Thanks for your quick reply!
    Anyway, the slug is variable.
    It changes every time the user choose a link, so how can I indentify it?

    I’ll make an example to be clear:

    https://www.mysite.it/page1/slug1
    https://www.mysite.it/page1/slug2
    https://www.mysite.it/page1/slug3

    And so on.
    slug1, slug2 and slug 3 are different in characters, lenght and they are not standard slug used by wordpress (e.i. %post%, %time% or whatever)

    How can I match them? using RegEX?

    If the variable slug is dynamically generated and changes each time a user chooses a link, it becomes more challenging to identify and remove it. In such cases, you might need to resort to regular expressions to dynamically match and replace the variable slug.

    Here’s a more generic example using the post_type_link filter and regular expressions to identify and remove the variable slug:

    function remove_variable_slug($permalink, $post, $leavename) {
        // Define the pattern to match the variable slug
        $pattern = '/\/page1\/([^\/]+)/';
    
        // Use preg_replace to remove the variable slug
        $permalink = preg_replace($pattern, '/page1/', $permalink);
    
        return $permalink;
    }
    
    add_filter('post_type_link', 'remove_variable_slug', 10, 3);

    In this example, the regular expression \/page1\/([^\/]+) is used to match the variable slug after /page1/. The ([^\/]+) part captures one or more characters that are not a forward slash. The preg_replace function is then used to replace the matched portion with /page1/.

    Keep in mind that regular expressions can be powerful but also need to be carefully crafted to match the specific patterns you’re dealing with. Adjust the regular expression according to the actual structure and patterns of your variable slugs.

    Thank you .

    Thread Starter mobius01001

    (@mobius01001)

    I’ve tried several times but my address bar still doesn’t change.
    Do I need to rewrite or flush all permalinks?
    I’ve also tried your first code (wich was simpler) , but still no luck.
    I am pretty sure I am missing something.
    Does post_type_link filter works on ALL dynamic elements created by user request right?
    Anyway, I am talking about this specific section of the site: https://www.photostaffparma.it/servizi
    I want to remove the album name from slug

    Anyway, thanks for you kindness, really appreciate it

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Edit Slug in permalink’ is closed to new replies.