Support » Plugin: Permalink Manager Lite » conditional permalink

  • Resolved almeidafelipe

    (@almeidafelipe)


    Hello, i have a structure that works like this:

    mydomain.com/custom-taxonomy-1/category/post-name

    But I have 3 taxonomy groups:

    . custom-taxonomy-1
    . custom-taxonomy-2
    . Category

    I would like to know if it is possible to have a different structure if I choose a particular item from custom-taxonomy-1. For example:

    If I choose any item from custom-taxonomy-1 the structure will be normal:

    mydomain.com/custom-taxonomy-1/category/post-name

    But if i choose a SPECIFIC item from custom-taxonomy-1 the structure will be different:

    mydomain.com/custom-taxonomy-1/custom-taxonomy-2/post-name

    I have the alternative of creating a custom post and defining this alternative structure for him, but I didn’t want to do that because the posts have the same architecture. It was just to have a different permalink structure.

Viewing 9 replies - 1 through 9 (of 9 total)
  • Plugin Author Maciej Bis

    (@mbis)

    Hi @almeidafelipe,

    To make the post permastructures conditional you will need a custom snippet.

    function pm_conditional_permastructure($permastructure, $post) {
    	// Do not change native permalinks or differnt post types
    	if($native_uri || $post->post_type != 'post') { return $default_uri; }
    
    	// A. Any term in 'custom-taxonomy-1' taxonomy
    	if(has_term('', 'custom-taxonomy-1')) {
    		$permastructure =  "%custom-taxonomy-1%/%category%/%postname%";
    	}
    	// B. Specific term in 'custom-taxonomy-1' taxonomy
    	else if(has_term('specific-term', 'custom-taxonomy-1')) {
    		$permastructure =  "%custom-taxonomy-1%/%custom-taxonomy-2%/%postname%";
    	}
    
    	return trim($permastructure, "/");
    }
    add_filter('permalink_manager_filter_permastructure', 'pm_conditional_permastructure', 10, 2);
    Thread Starter almeidafelipe

    (@almeidafelipe)

    Amazing! In this case where there is ‘specific-term’ should i replace it with the “custom-taxonomy-1 item slug”?

    You are a genius!

    Plugin Author Maciej Bis

    (@mbis)

    Yes, indeed ๐Ÿ™‚

    Plugin Author Maciej Bis

    (@mbis)

    Hi @almeidafelipe,

    The version of snippet that I sent you includes a tiny error. Here is the correct version:

    function pm_conditional_permastructure($permastructure, $post) {
    	// Do not change native permalinks or differnt post types
    	if($native_uri || $post->post_type != 'post') { return $permastructure; }
    
    	// A. Any term in 'custom-taxonomy-1' taxonomy
    	if(has_term('', 'custom-taxonomy-1')) {
    		$permastructure =  "%custom-taxonomy-1%/%category%/%postname%";
    	}
    	// B. Specific term in 'custom-taxonomy-1' taxonomy
    	else if(has_term('specific-term', 'custom-taxonomy-1')) {
    		$permastructure =  "%custom-taxonomy-1%/%custom-taxonomy-2%/%postname%";
    	}
    
    	return trim($permastructure, "/");
    }
    add_filter('permalink_manager_filter_permastructure', 'pm_conditional_permastructure', 10, 2);
    Thread Starter almeidafelipe

    (@almeidafelipe)

    Wooow! Great!

    I am afraid to use this code and in the future the plugin or wordpress will change and the code will stop working.

    In this case you will make a correction of the plugin but what about the code? Will it stop working in the plugin and everything will be messy? Excuse me for the question, but i was a little scared now.

    What do you think?

    Plugin Author Maciej Bis

    (@mbis)

    The snippet I sent will work only if my plugin is activated and would filter only the default permalink format. It will not break your website for sure ๐Ÿ™‚

    WordPress generates the permalinks dynamically (they are not saved in the database) – my plugin stores all the custom permalinks statically in the database.

    Whenever you publish new post, my plugin will use the existing permalink structure to generate and save the particular permalink. It means that even if you remove this snippet (or if it will no longer work for any reason), the permalinks saved prior to this moment will remain unaffected. That is because my plugin have already saved them.

    Of course you can manually change each of them using included URI editor at any moment:
    https://permalinkmanager.pro/docs/basics/manually-edit-wordpress-custom-permalinks/

    The other important thing is that if you would like to restore the original/native permalink settings all you need to do is to simply deactivate my plugin.

    Thread Starter almeidafelipe

    (@almeidafelipe)

    Many thanks for the reply. One last question:

    I’m asking these things because i’m setting up a columnist area on my blog. But i need a different permalink structure for these columnist posts. Do you think that it is better to use this snippet that you provided (which changes the functioning of your plugin) or just create a custom post and use your plugin’s custom posts native permalink system?

    Plugin Author Maciej Bis

    (@mbis)

    Hi @almeidafelipe,

    I think that the most convenient way would be to register a new custom post type.

    Thread Starter almeidafelipe

    (@almeidafelipe)

    Great. Thank you again.

Viewing 9 replies - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.