Wednesday, May 27, 2009

Apache: "Redirecting" Missing Images with a Default Image

We all know how horrible sites with broken image links are. If you maintain a lot of sites, you can't give your attention to each and every image detail on every site. So what about displaying a default image instead when an image is missing? Well, that will work perfectly fine as long as the images are stored on your server and are not external image links; and this is real simple to do with Apache's mod_rewrite!

Try this code in your vhost entry:

RewriteEngine On
RewriteCond "%{DOCUMENT_ROOT}%{REQUEST_URI}" !-f
RewriteRule \.(gif|jpe?g|png|bmp) /path/to/your/404.gif [NC,L]


Cheers!

Note: This idea was found on the web. At the time of this writing, I cannot find the original source. But if I do, I will properly credit them!

Update A forum link was found as a reference:http://www.webmasterworld.com/apache/3274493.htm

Wednesday, May 20, 2009

Getting Unique Entries From Two Files

Okay, so you have data in one file and then you have updated data in another file. Let's say you have a list of directories that have been searched and they all have a filename, "0.txt". Another file lists all of the directories that have this "0.txt" file but they are actually empty. If you diff the files that will only give you changes being made and won't necessarily give you the entries NOT in the updated file. But, if you get all of the directory entries that are NOT in the new list, then you have a complete list of directories that have a valid file!

*(Well, that was just my example, my real life scenario involved MySQL data.)

Anyhow, with this PHP code you can basically go through both files and utilize in_array to see if certain entries exist in one file or not.

**Note: I wanted to use $argv to handle the arguments, but for some reason file didn't like it. Curious...

$f1 = "/tmp/original.txt";
$f2 = "/tmp/modified.txt";

$a = file($f1);
$b = file($f2);

foreach ($a as $k => $v) {
if (!in_array($v, $b)) {
print "$v";
}
}


Cheers!

Friday, May 8, 2009

WordPress: Modifying Post Content Automatically

Have you ever had the need to have specific content on every single blog post? The way that sounds, I'm sure you'll probably say, "no". But, think about it now, the content does include the HTML. OIC...

So here at work, I've gotten a request to have all links be passed through a link tracker that we built in-house. "Why?" Well, we want to do some sort of testing so we want to keep track of all clicks on links. So that basically means, we need to change all URIs to have the link tracker URI appended by the actual URI that the user put.

Not bad, we can use the WordPress filter, "the_content", and that will get the job done!

Now let's look at the trackURL function:

preg_match_all("/href=\"(.*)\"/",$content, $urls, PREG_PATTERN_ORDER);
$uniqueurls = array_unique($urls[1]);
foreach ($uniqueurls as $url) {
if (!preg_match('/trackerUri.com/', $url)) {
$content = str_replace($url, "http://trackerUri.com/?ab=c/hij&ku=" . $url, $content);
}


It is straightforward; it goes through the content that it is given and finds any "href" references. After those are found, it does a search & replace to insert the new tracker URI before the user-inputed URI.

The rest utilizes WordPress's built-in functions to update the post_content. Easy!

Cheers!


function trackURL($content) {
global $post;
preg_match_all("/href=\"(.*)\"/",$content, $urls, PREG_PATTERN_ORDER);
$uniqueurls = array_unique($urls[1]);
foreach ($uniqueurls as $url) {
if (!preg_match('/trackerUri.com/', $url)) {
$content = str_replace($url, "http://trackerUri.com/?ab=c/hij&ku=" . $url, $content);
}
}
return $content;
}

function trackContent($content) {
global $post;
$pcontent = array();
$pcontent['ID'] = $post->ID;
$pcontent['post_content'] = clicktrackURL($content);
wp_update_post($pcontent);
return $content;
}
add_filter('the_content', 'trackContent');

Wednesday, May 6, 2009

WordPress: update_option ping_sites via PHP CLI

I really hate relying on other site's servers for anything. For obvious reasons, if their site is slow, well that means whatever functionality you're relying on will be slow as well.

In this latest case, technorati.com's blog update service was timing out 'causing our WordPress posts to take up to 2 minutes. Not fun! So, sorry technorati, we're replacing you with google's update services until you get your act together. :)

Now, why am I doing this via CLI? Well, I manage 300+ WordPress blogs, so with a bash wrapper, I can give this script the site and then it will connect and run an update_option and we're good to go!

Yay!


include '/mnt/www/wordpress/wp-config.php';
global $wpdb;
$pingsites = get_option('ping_sites');
$newpingsites = str_replace('http://rpc.technorati.com/rpc/ping','http://blogsearch.google.com/ping/RPC2',$pingsites);
$newpingsites = str_replace('http://rpc.icerocket.com:10080/','',$newpingsites);
echo $newpingsites . "\n";

echo "updating $argv[1]'s ping_sites\n";

update_option('ping_sites', $newpingsites);

}