Codex

User:DavidHouse/WP Rewrite API

Possible API for WP_Rewrite to make it easier for plugins to interact with it.


Contents

add_rewrite_rule()

A function for adding a straight rewrite rule.

Parameters:

  • Regex to match
  • URL to redirect to.
  • Optional flag indicating if rule should be added to top or bottom (default) of the rules.

The URL should look like 'index.php?var1=$matches[1]&var2=$matches[2]&var3=$matches[3]' where var1-3 are the relevant query variables and $matches refers to matches in the regular expression.

What it needs to do:

  • Build a rewrite rule from the regex and redirect URL.
  • Theoretically it could then filter rewrite_rules_array and add the rules itself, but I think we should add a function to WP_Rewrite for adding 'extra' rules that needn't be generated by generate_rewrite_rules(). rewrite_rules() would then just tack on these extra rules after the ones generated. These extra rules should probably be stored in an option.

add_rewrite_tag()

Add a new tag (like %postname%):

Parameters:

  • Name of tag
  • Regex to match

What it needs to do:

  • Get a query var name by stripping the % signs from the name of the tag: trim($name, '%')
  • Call $wp_rewrite->add_rewrite_tag() with the name, generated QV name and regex.
  • Add the QV as a query var (again, this could be done by filtering query_vars but it might be nicer to add a function to the WP class that stores 'extra' QVs like above)

Notes: I was thinking about passing a callback to this function which would be called with the contents of the QV when a request matched the regex (so e.g. if the regex is ([0-9]{3}) as in skippy's case, we could accept a callback to call when someone requests a Julian date, say, /2006/32, passing 32), but I don't think this will be flexible enough: people might want to hook onto parse_request, or pre_get_posts, or parse_query, or many other hooks. So we can just do the $wp_rewrite->add_rewrite_tag() bit and leave it up to the query author to add their own hooks.


add_rewrite_endpoint()

Add a new endpoint like /trackback/

Parameters:

  • Name of endpoint
  • Places mask which indicates on which links the endpoint should be added (EP_PERMALINK, EP_PAGES, EP_ATTACHMENT, etc).

This adds the endpoint to all link types indicated (e.g. posts, pages, category, author, search) and then template-loader.php includes the relevent handler file

The name of the endpoint is added as query variable and this gets as value any text present after the endpoint name, separated from the name with a '/'. The template_redirect handler should test this query variable.

This could be used for all sorts of things:

  • ajax handler
  • form submission handler
  • alternative notification handler
  • ...


--Westi 22:08, 5 Feb 2006 (GMT)


add_feed()

Add a new feed type like /atom1/

Parameters:

  • feed
  • File to include to handle feed / or should it be a function to call / either

This simply adds/overrides the feed type and then wp-feed.php includes the relavent handler file

--Westi 22:06, 5 Feb 2006 (GMT)


add_base()

Add a new base permalink type (equivalent of author, archives, category etc. or equivalent of search)

Parameters:

  • permalink name
  • normal query (bool) - If true then this base supports a normal query type - ie the same child permalink structure as archives. If false then the handler will parse the rest of the query.
  • File to include to handle it / or should it be a function to call / either

This simply adds a new permalink base type. Either with or without the standard permalink appendage. It could be used for a /tags/tagname type structure, or to embedded a gallery or for something like /wibble/2005/12/3 where you wanted to put a particular spin on the query results

--Westi 22:06, 5 Feb 2006 (GMT)


Examples

To do something similar to what skippy was asking for, you'd do this:

http://xmouse.ithium.net/dmh-source/wp-rewrite-api.phps