0

I'm trying to map a path such as

/BigOffer12345

to an actual page such that the numeric part is passed as an argument.

/Offers/BigOffer/?id=12345

The id passed in will ultimately be used to generate custom content pulled from a remote source (similar to a purl service).

I've been looking into Rewrite, but I'm new to WordPress and I think I'm getting stuck on some basics. Does something like this need to happen in a plugin or is there another interface to get this into WordPress?

1

If BigOffer part is static it can be easyly done with a rewrite rule:

add_action('init', 'big_offer_rule');

function big_offer_rule() {
  add_rewrite_rule('^BigOffer([0-9]+)/?','index.php?pagename=bigoffer&offerId=$matches[1]','top');
}

add_filter('query_vars', 'big_offer_vars');

function big_offer_vars( $vars ) {
  return array_merge($vars, array('offerId') );
}

After you added this code, you have to flush rules going in Settings -> Permalinks section in your backend and saving changes.

After that, you have to create a page with the slug: 'bigoffer'. This page will be opened when you type an url like http://example.com/BigOffer12345 and the numeric part can be used looking at get_query_var('offerId'), something like:

add_action('template_redirect', 'big_offer_id');

function big_offer_id() {
  if ( is_page('bigoffer') ) {
    // do something... following is just an example
    // the function get_the_content_somewhere_by_id does not exist,
    // just imagine is a function that retrieve the page content using the id
    $offerid = get_query_var('offerId');
    global $offer_content;
    $offer_content = get_the_content_somewhere_by_id($offerid);
  }
}

I've used the hook 'template_redirect' so everything you do in the function is done before the page is displayed. Then I used a global variable to store the content retrieved, in this way in the page template you can just global $offer_content; echo $offer_content;.

Off course is not real code is just a proof of concept.

All this code, can be wrote in a custom plugin (suggested place), or in the functions.php of the current theme.

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.