Tag Archives: parameter

WordPress PHP: How to add URL Querystring Parameters to Your Plugin CodeUnit 26 DEC 2011

WordPress is set up such that unknown URL GET parameters are ignored, a nice security/navigation feature to be sure, but an annoying one if you are developing a plugin that needs to allow for GET variables in order to control your output.

Luckily there is a solution to achieve this though… to add new allowed URL parameters to your WordPress site, simply extend the query_vars filter! :)

In practice (you add this at the top of your plugin code):

add_filter('query_vars','wtap_queryvars');

function wtap_queryvars($qvars){
    $qvars[]='wtap_month';
    $qvars[]='wtap_year';
    $qvars[]='wtap_categories';
    return $qvars;
} 

Note that it is a good idea to give your accepted GET parameters unique names so that you won’t clash with already accepted and processed GET variables. In the example above, I’ve gone and prefixed everything with wtap_.

Now that your site allows GET parameters, you obviously need to make use of them. To do this, first check their existence and then grab them via the wp_query global object:

global $wp_query;
if (isset($wp_query->query_vars['wtap_year'])){
echo '<p>Year passed in url: ' . $wp_query->query_vars['wtap_year'] . '</p>';
}

Great. So now http://yoursite.com/plugin-page/?wtap_year=2011&wtap_month=12 will throw up something valid, instead of an annoying 404 page!

Nifty.

PHP: Save print_r output into a Variable CodeUnit 10 MAR 2011

print_r() is an extremely useful PHP function that displays the information about just about any type of variable in a way that’s readable by humans. It is particularly essential for the display of arrays, making it often directed towards uncovering those all important system arrays of $_GET, $_POST, $_SERVER and of course, $_SESSION.

However the function by default prints out the information, sometimes not always the most useful of behaviours if you want to return its result as part of a function. However, not all is lost if you know about its second optional parameter, called $return.

$return is defaulted to false, meaning the function follows its default behaviour of printing out the information. However, if you set it to true, print_r() will in fact return what it would have printed as a string, perfect for stuffing into a variable and using elsewhere.

In practice:

$string = print_r($_GET,true);
echo $string;

Nifty.

Related Link: http://php.net/manual/en/function.print-r.php

How to Download to a Specific Directory using Wget for Windows Tips, Tricks and Tutorials 26 APR 2010

The Wget for Windows application is of course exactly that: the classic GNU/Linux wget application built for Windows command line usage.

Of course, this means that most if not all command line switches are still available to you, just as what they would be had you been running the latest wget from your Linux distro of choice.

The question I’m answering today is how to specify which directory wget is to save what it is downloading to.

Normal usage sees wget placing whatever it grabs in the directory from which it is called.

You can of course specify a specific file to which to save your downloaded web page when downloading a single page by making use of the -O switch, resulting in something that looks like this:

"C:\\wget.exe" -O "C:\\downloadpage.txt" "http://www.download.co.za/index.html"

If however we want to specify the directory to which wget must save its downloaded content to, we need to make use of the -P switch, which the results in a call looking like:

"C:\\wget.exe" -P "C:\\download" "http://www.download.co.za/index.html"

So at the end of the operation specified above, we would be sitting with a file named index.html in the directory C:\\download.

And that’s how you specify to which directory wget must drag its captures down to!

Related Link: http://gnuwin32.sourceforge.net/packages/wget.htm