I have a plugin that uses permalinks to deliver content to the page based on an activity that I pass in through the URL, eg. mysite.com/activity/11.
I also have code in my plugin that redirects the user to the most recent activity if they don't enter it. eg. mysite.com or mysite.com/activity would redirect to mysite.com/activity/11.
Up until recently, this has been working great. However when I try to update to WordPress 4.5 this functionality completely breaks and users end up in a redirection loop.
<?php
function co3_add_vars($public_query_vars)
{
if ( get_option('permalink_structure') != '' ) {
$public_query_vars[] = 'activity';
}
return $public_query_vars;
}
add_filter('query_vars', 'co3_add_vars');
function co3_course_rewrite()
{
$co3_pages = get_option('campus_site_pages');
$home = $co3_pages['course'];
add_rewrite_rule('activity/([^/]+)/?$', 'index.php?page_id=' . $home . '&activity=$matches[1]','top');
add_rewrite_rule('activity/?$', 'index.php?page_id=' . $home, 'top');
}
add_action('init', 'co3_course_rewrite', 10, 0);
I've flushed the permalink tables and I've double-checked the rewrite rule matches with the Rewrite Analyzer plugin.
Another thing I noticed is that redirect_canonical( )
is firing before any of my plugin code does.
Simply put, I need /activity/11 not to redirect to /.