0

I have created a function as follows in the functions.php page (from questions asked here):

add_filter('rewrite_rules_array','job_rewrite_rules_array');
function job_rewrite_rules_array($rules) {      
    $job_page = get_post(123); 
    $job_rules = array();
    if( is_object($job_page) ){
        $job_slug = $job_page->post_name;
        $job_rules[$job_slug.'/([^/]*)/([^/]*)$'] = 'index.php?pagename='.$job_slug.'&param1=$matches[1]&param2=$matches[2]'; 
    }
    return $job_rules + $rules;
}

// Bind Query Var
add_filter('query_vars','job_query_vars');

function job_query_vars($vars)
{
        array_push($vars, 'param1');
        array_push($vars, 'param2');
        return $vars;
}

function job_rewrite_flush(){
    global $wp_rewrite;
    $wp_rewrite->flush_rules();
 }
add_action('init','job_rewrite_flush');

What I have hoping to do is change the $job_slug from page data dynamically based on the pageID as follows:

By changing this line $job_page = get_post(123);

to this code:

    if( is_page(123) ) 
        $job_page = get_post(123);
    else if( is_page(456) ) 
        $job_page = get_post(456);

In other words, I wan to get different page data so that the function will work for different pages.

Is this possible? The code above is not working and the page is not refreshing the content when it is called. The page is resubmitted through a javascript function for example (p is a value of some other variable not related to the pageid):

if(p == 333)    
   window.location = "<URL HERE>/product-range/" + val1 + "/" + val2;
else if(p == 444)   
   window.location = "<URL HERE>/ano-product-range/" + val1 + "/" + val2;

val1 and val2 are 'mapped' to the param1 and param2 in the function on the functions.php page.

From reading on the forum here I guess I am trying to access the PageID on the functions.php page but maybe this is not possible as the POST data has not loaded at this stage?

  • rewrite rules should be added and flushed once, typically on plugin or theme activation, or on the admin side when data changes. trying to do this on the actual requests for these pages is the wrong approach. – Milo Jul 20 '18 at 16:32

Your Answer

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

Browse other questions tagged or ask your own question.