The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End Can't get WordPress custom rewrite rules to work

  • This topic is empty.
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
  • #243015
    Ilan Firsov

    I’m trying to create a custom rewrite rule for a file download
    The rewrite rule itself is working, but I can’t get any custom parameters (rewrite tags) to work.

    So this is my test plugin:

    add_action('init', 'my_init', 10, 0);
    function my_init() {
    function my_add_rewrite_tags() {
        add_rewrite_tag( '%test%', '([^&]+)' );
    function my_add_rewrite_rules() {
        add_rewrite_rule( 'test/([^/]+)/?', 'index.php?pagename=sample-page&test=$matches[1]', 'top' );
    function my_maybe_flush_rewrite_rules() {
        // no_rewrite_rule_flush_needed transient is removed on plugin activation/deactivation
        if ( ! get_transient( 'no_rewrite_rule_flush_needed' ) ) {
            set_transient( 'no_rewrite_rule_flush_needed', true );
    function my_maybe_start_download() {
        global $wp_query;
        $test = $wp_query->get( 'test' );

    I can see the sample-page page when I navigate to localhost/test/something so the rewrite rule appears to be working, but I can’t get the value for test parameter.
    Any ideas how to get this to work?

    Ilan Firsov

    Looks like I’ve got it to work.
    First thing I moved the call to maybe_start_download() to the wp action hook.
    At first it still didn’t work so I changed add_rewrite_tag( '%test%', '([^&amp;]+)' ); to add_rewrite_tag( '%test%', '([^/]+)' ); and then it started working.
    The interesting thing is that it works now even if I change the regex in add_rewrite_tag back to ([^&amp;]+) it still works.

Viewing 2 posts - 1 through 2 (of 2 total)
  • The forum ‘Back End’ is closed to new topics and replies.