One thing that I knew I wanted to be able to do with this plugin is to mimic the behavior of any Gallery2 site – namely, the ability to show albums and images with thumbnails and the users being able to click on an album to open it up in a new page.
In order for me to do that, what I needed to be able to do is pass and receive query variables from the URI request. This way, I could setup my link tags to include the appropriate variables and my plugin could received them when the page is loading, process them, and display the correct album content.
It turns out that doing this is pretty simple. What helped me a lot was looking through the NextGen Gallery Plugin code (which, is another excellent gallery plugin – I am currently using it on my site).
In order for WordPress to accept our query variables and parse them correctly, there are three things that need to be done:
- Hook a callback to the parse_request action and the query_vars filter. This is done with the following two lines of code placed in the plugin class constructor (or the initialization function, if you are not using a class):
add_action("parse_request", array(&$this, "process_request")); add_filter("query_vars", array(&$this, "add_wg2_query_vars"));The first callback – parse_request – will be called after the query has been parsed by the WordPress core and there we’ll be able to extract the values of the variables we are interested in.
The second one – add_wg2_query_vars – will allows us to tell WordPress what our variables are so it knows to parse and store them (WordPress ignores all request variables that it does not recognize for security reasons). - Write the callback that tells WordPress what our variables are. This is as easy as adding elements to a PHP array. The callback that we hooked to the query_vars filter will be called with the array of query variables already recognized as the only argument. The only thing we have to do is add our variables and return the modified array:
/** * add_wg2_query_vars * Add all the variables that we want to have parsed by the WP core. * This is the callback that we've registered for the "query_vars" * filter. "Our" variables will be used by the "parse_request" action * callback. * * @param array $qvars The array containing all query variables * @return The modified array */ public function add_wg2_query_vars ($qvars) { $qvars[] = "wg2_album"; $qvars[] = "wg2_image"; $qvars[] = "wg2_page"; return $qvars; } - Lastly, we have to write the callback that we get any values that we are interested in out of the parsed query. Again, this is just as easy ad reading variables out of an array. The only argument that will be passed to our callback is a reference to the global $wp object. I am not entire sure what that object encompasses but I know that one of it’s properties is the parsed query:
/** * process_request * Check the query_vars member of the $wp object for any of "our" * query variables and save them into our object. * * @param object $wp * @return void */ public function process_request($wp) { if (array_key_exists ("pagename", $wp->query_vars)) $this->query["page"] = $wp->query_vars["pagename"]; if (array_key_exists ("wg2_album", $wp->query_vars)) $this->query["album"] = $wp->query_vars["wg2_album"]; if (array_key_exists ("wg2_image", $wp->query_vars)) $this->query["image"] = $wp->query_vars["wg2_image"]; if (array_key_exists ("wg2_page", $wp->query_vars)) $this->query["page"] = $wp->query_vars["wg2_page"]; }
This is it! Now, we can setup links which include our three query variables as part of them and the WordPress core will parse them and let us use them.
What this means is that I can setup link like this:
<a href="/gallery_page.php?wg2_album=123&wg2_page=3">Page 3</a>
and when the user clicks on that link, my plugin will know that it needs to show Page 3 of album with ID 123.
Popularity: 24% [?]