Wordpress Rewrite Rule to Pass Custom Query Var to Homepage

12 Views Asked by At

I want to rewrite all page requests back to the homepage as a custom query var. For example, /about/ becomes /?anchor=about. While the below rewrite rule does return me to my homepage, it does not include the query var. It doesn't have to be a query_var, but I can't seem to pass it as a URL parameter either.

/** 
 * Register Custom Query Vars
*/

function register_custom_query_vars($query_vars){

    $query_vars[] = 'anchor';
    
    return $query_vars;
    
}

add_filter('query_vars', 'register_custom_query_vars');

/** 
 * Register Custom Rewrite Rules
*/

function register_custom_rewrite_rules($rules){

    $pageID = get_option('page_on_front');

    $new_rules = [
        '([a-zA-Z0-9\._-]+)/?$' => 'index.php?page_id='+$pageID+'&anchor=$matches[1]',
    ];
    
    $rules = $new_rules + $rules;
    
    return $rules;

}
 
add_filter('rewrite_rules_array', 'register_custom_rewrite_rules');
0

There are 0 best solutions below