I'm trying to store query variables to a localized variable in the frontend of my plugin. rpSearch shows up like it should but page and share_id are "0" and "" respectively when I test the url with Monkeyman Rewrite Analyzer it looks like it should give me the data i need

screenshot from monkeyman rewrite analyzer

function rewrite() {
    add_rewrite_rule('^reseplanerare/dela/([^/]+)/?$','index.php?pagename=reseplanerare&page=share&share_id=$matches[1]', 'top');
    add_rewrite_rule('^reseplanerare/planera/([^/]+)/?$', 'index.php?pagename=reseplanerare&page=plan', 'top');

function register_query_vars( $vars ) {
    $vars[] = 'page';
    $vars[] = 'share_id';
    return $vars;

function public_enqueue_scripts() {
    global $wp_query;
    wp_enqueue_script( $this->plugin_name,  plugin_dir_url( __FILE__ ) . 'js/public/main.js#module', array('jquery'), $this->plugin_ver,true);
    wp_localize_script( $this->plugin_name, 'rpSearch', array(
        'page' => urldecode($wp_query->query_vars['page']),
        'share_id' => urldecode($wp_query->query_vars['share_id'])

I wasn't linking to the page correctly in the rewrite rules. The use of pagename was incorrect instead I should have used page_id. The rewrite rules I ended up using was

    add_rewrite_rule('^resa/start/?$', 'index.php?page_id=1897&route_page=start', 'top');
    add_rewrite_rule('^resa/planera/?$', 'index.php?page_id=1897&route_page=plan', 'top');
    add_rewrite_rule('^resa/dela/([^/]+)/?$','index.php?page_id=1897&route_page=share&share_id=$matches[1]', 'top');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.