Simple WordPress plugin debugging
I’d like to present simplest way I know to debug WordPress plugins. It is a method I was using developing plugin to cloak URL‘s described earlier.
Below You can find the most minimalistic example plugin code possible – all it does is log REQUEST_URI to our debug file located at wp-content/plugins/debug. Row by row. Dirty and simple – and the most important – it does not require us to switch our WordPress to debug mode.
Plugin consists of three parts – plugin description header, debugging function and hook to main function which is executed right before page is rendered.
< ?php
/* Plugin Name: test Plugin URI: Description: Version: Author: Author URI: */
?>
< ?php
function debug($msg)
{
$fp = fopen('wp-content/plugins/debug', 'a');
fwrite($fp, $msg."\n");
fclose($fp);
}
function main()
{
debug($_SERVER['REQUEST_URI']);
}
add_action('init', 'main');
?>
Of course the most important part of the code is debug function which stores results we need to verify to our debug file.
WordPress plugin to redirect or cloak URL’s
After few unsuccessful searches for free WordPress plugin that would allow me to cloak URL’s on my blog – I decided to write my own. It was not really hard. I’m publishing it on GPLv3 license.
Plugin is really simple – full code consists of 75 lines of code. It does not contain database support, hits counting nor wp-admin interface. If some of You will like to extend it including those features – please do it and inform me – I will be first betatester
Plugin assumes that You already accomplished redirecting URL’s to WordPress subsystem that You would like to be handled by WordPress in a different way.
There are three possible kinds of URL threating:
- Permanent redirect – typical
HTTP 301 redirect - Temporary redirect – typical
HTTP 302 redirect - Local cloak – it works only on Your blog web page (thats why it is called local) – it will hide real destination URL leaving it cloaked
How does it work?
The core of the plugin source is pasted below. Maybe some of You will consider it as interesting
The most unfortunate part of the plugin is need to use /wp-admin/plugins.php Edit plugin feature to manually change URL’s. Those are listed inside $redirect variable.
define('PERMANENT_REDIRECT',301);
define('TEMPORARY_REDIRECT',302);
define('LOCAL_CLOAK',1);
$redirects = array(
# source match TYPE destination url
# /u
"^/u$" => array(TEMPORARY_REDIRECT, "/u/"),
"^/u/(.*)$" => array(LOCAL_CLOAK, "/category/u/$1"),
}
function url_cloaker()
{
global $redirects;
$src_url = $_SERVER['REQUEST_URI'];
foreach ($redirects as $src => $dst) {
$src = str_replace('/','\/',$src);
if (preg_match('/'.$src.'/',$src_url)) {
#__d('Matched: '.$src);
$dst_url = preg_replace('/'.$src.'/', $dst[1], $src_url);
switch ($dst[0]) {
case PERMANENT_REDIRECT:
wp_redirect($dst_url, PERMANENT_REDIRECT);
exit;
case TEMPORARY_REDIRECT:
wp_redirect($dst_url, TEMPORARY_REDIRECT);
exit;
case LOCAL_CLOAK:
$_SERVER['REQUEST_URI'] = $dst_url;
break;
}
}
}
}
add_action('init', 'url_cloaker');
Plugin use actions hooking framework inside WordPress to make sure it is fired before page is shown.
The code above seems to be unreadable for You? Get this PHP book, because PHP is very simple language to learn and it offers a lot of new possibilities for those who know how to use it.
