Trouble with add_rewrite_rule() handling

Viewed 19

I am working on a site-specific plugin that handles requests of the form /is/myProductName. I am trying to get WordPress to handle these dynamic URLs but am having trouble. I have been working most directly off the answer on this thread, https://wordpress.stackexchange.com/questions/212856/dynamic-url-not-a-physical-page-within-the-database.

Here's what I have in my main plugin file

add_action( 'init',  function() {
    add_rewrite_rule( '^is/([^/]*)/?$', '/index.php?package_tag=$matches[1]&__generate_is=1',  'top' );

    //temporarily flushing permalinks on reload
    global $wp_rewrite;
    $wp_rewrite->flush_rules();
} );


add_filter( 'query_vars', 'pmg_rewrite_add_var', 100 );
function pmg_rewrite_add_var( $vars ) {
    $vars[] = 'package_tag';
    $vars[] = '__generate_is';
    return $vars;
}

add_action('parse_request','sniff_requests', 100 );

function sniff_requests($wp_query) {
   global $wp;
   mylog($wp->query_vars); //mylog is a custom log function using print_r()

   if(isset($wp->query_vars[ '__generate_is' ])) {

       if(isset($wp->query_vars[ 'package_tag' ])) {
           $this->handle_generate_is();
           die(); // stop default WP behavior
       }
   }
}

function handle_generate_is() {
    // use the plugin index.php file to handle request

    add_filter('template_include', function ($original_template) {
        return(__DIR__ . '/index.php');
    });
}

The line that logs query_vars returns the following:

Array
(
    [p] => 13 
    [attachment] => myProductName
)

13 is the id of my home page. So it looks like the most likely part that isn't working is the first function adding the rewrite rule. Does anyone see what I'm doing wrong? I'm stuck.

0 Answers
Related