A couple weeks back I received a very interesting request. A user wanted to redirect to different pages based on the category selected in Connections. After fiddling with a couple different ways of trying to achieve this, I came up with a fairly simple elegant solution that is easy to maintain.
Before moving on, the first thing you should do is install the Code Snippets plugin.
After you install the Code Snippets plugin, add a new snippet with the following code. Save the snippet but do not activate it yet.
function cn_category_select_redirect() { if ( isset( $_REQUEST['cn-cat'] ) ) { $cnCatID = absint( $_REQUEST['cn-cat'] ); $redirectMap = array( x => y, // `x` equals the Connections Category ID and `y` equals the WordPress Page ID. // Additional redirects can be added by duplicating the above row as many times as needed and replacing this row. ); if ( isset( $redirectMap[ $cnCatID ] ) ) { wp_redirect( get_permalink( $redirectMap[ $cnCatID ] ) ); exit; } } } add_action( 'template_redirect', 'cn_category_select_redirect' ); |
If you copy paste the above code, you should change the function name cn_category_select_redirect
. Function names should always be unique. If you do not make sure of this, you will crash your site. If you do, the Code Snippets plugin has a safe mode which can be enabled so you can fix it. Read the Code Snippets FAQ on how to enable safe mode if you need to. Also, you should not activate the snippet yet. Doing so could cause an error because the x,y
need to be changed to actual numbers.
The key to making this work is to replace the x
with Connections Category ID number and the y
with WordPress Page ID numbers. The Connections Category ID number can be found on the Connections : Categories admin page.
I suggest writing all the ID numbers you want to apply a redirect to on a piece of scratch paper. Now you need to get the WordPress Page ID numbers. This is a little bit more tricky because WordPress “hides” this information. Probably the easiest way to get the page ID number is to edit the page to which you want to add a redirect. If you look in the browser address bar you will find the page ID.
No go back to the new code snippet you added…
Replace the x
with a Connections Category ID and replace the y
with the WordPress Page ID it should redirect to. You can add as many redirects as you wish by duplicating this line and changing the category ID and the page ID.
Finally, you can activate your new snippet and give it a test.