Mastodon Autopost Plugin

I saw this plugin mentioned on Mastodon today and thought I would give it a try on ClassicPress and see if it worked. Install in ClassicPress went easily, like most WordPress plugins do. There were no warnings and ClassicPress says it is compatible. The only hard part was finding the plugin. I had to search by the plugin authors name to find it. I have to say the authors name, L1am0, does not inspire confidence, but their website looks legit enough.

Once installed and activated I found the settings for the plugin under the Settings menu, which is nice. So many plugins think they deserve their own spot the admin menu, nice to see a plugin that picks the right menu place.

To get started you will need to connect your website with your Mastodon account. Simply start typing the name of your Mastodon server and the plugin will start to filter your choices down. Once you have your server selected you can then authorize the plugin as an app on your Mastodon server. After you authorize the plugin you will be redirected back to your website. This is the only error I had. The plugin output some text and cause a PHP bad header. I had to reload the admin and go to the settings page again to continue.

You can then select if it posts when new posts, pages or media are created, and how it looks. I can’t tell you if the plugin really works yet, since this is the first post I have created since I installed it. Once I publish this post I will come back and let you know.

You can find the plugin in the WordPress plugin repository.

Well look at that. Looks like it works.

Disable the W3 Total Cache Footer Comment

Most of the WordPress/ClassicPress websites I work on use Cloudflare I don’t often have problems with the W3 Total Cache footer comments in the footer. Cloudflare usually just removes them. But recently I was helping a friend with a website that had the W3 Total Cache plugin installed and the footer comments were being inserted. They simply wanted to get rid of them. Thankfully a quick Google search lead me to a WordPress support comment with the solution.

Add the following code to your theme functions file, or a site specific plugin and you can easily turn off the W3 Total Cache footer comments for all visitors to your website.

add_filter( 'w3tc_can_print_comment', function( $w3tc_setting ) { return false; }, 10, 1 );

Cloudflare Location Check

Recently I had a client come to me with a very interesting problem. They have multiple web servers running behind a Cloudflare load balancer and the majority of those requests flow through the load balancer and are directed properly. However, there were still a number of requests that would make it through straight to the origin IP’s. The majority of those requests were often attempts at common WordPress exploits or simply plugin scanning looking for vulnerable plugins. If those requests were properly going through Cloudflare they would be dealt with by the Cloudflare firewall but by using the IP address these bad actors were able to bypass that layer of security.

Thankfully Cloudflare has a way to check to see if the request actually passed through Cloudflare if the website has turned on the Cloudflare IP Geolocation. That service automatically adds the country code value passed along in the CF-IPCountry request header to the origin web server. If you want to know more about the Cloudflare IP Geolocation check out their support document.

The logic is pretty simple, if the request does not have the CF-IPCountry request header then the request did not pass through Cloudflare (and the Cloudflare Firewall) so redirect the request back to the fully qualified domain name.

/**
* Cloudflare Location Check
*
* Checks for the Cloudflare location header. This is only there if the visitor has come through Cloudflare.
* If the request does not have this it is direct access and should be redirected to the host name
*
*/
function lgr_cflocation_check () {
//the country header is added by Cloudflare. If it is not there then this is direct IP access and needs to be redirected.
if( !$_SERVER["HTTP_CF_IPCOUNTRY"] ) {
//send them to the full URL which should add it.
header("Location: ".home_url().$_SERVER['REQUEST_URI']);
die();
}

}
add_action( 'init', 'lgr_cflocation_check' );

If you are having problems with direct access to your website through the IP address this might help you.

Geolocation Click Redirection

One of the affiliate offers I participate in redirects visitors to their website to their country specific based websites which is great for the user, not so great for me because when the user is redirected the affiliate cookie is lost and I don’t get credit for the sale. Each country has their own affiliate program so it is possible to get credit for any sales that happen on those country specific sites, but the redirect has to happen before the user visits the main company website where they redirect the user.

While it is possible to create country specific landing pages for each country there is always the chance that a visitor from one of those other countries will still click on a link that would take them to the main company website. To prevent this from happening I have installed a script to check geolocation and redirect the user to the appropriate country specific website instead of them going to the main companies website and being redirected.

Eric Nagel has a PHP script you can use to do something similar, and while his method would work the thought of having to maintain the IP Geolocation database, even through a cron job, just sounds like trouble to me. Not to mention some shared web hosts don’t allow you to run cron jobs or to run a cron job that unzips a file.

The solution I came up with relies on the service Cloudflare. I wrote a post about Cloudflare earlier this year. Cloudflare has this neat little feature called Geolocation where they will add in a server variable that you can access with PHP calle $_SERVER[“HTTP_CF_IPCOUNTRY”]. Once you turn the feature on in Cloudflare all the requests to your Cloudflare enabled website will contain the country code of the user.

All you have to do then is create a PHP redirect script that checks for the Cloudflare variable and redirect the user to the correct link.

Here is a copy of what I use to redirect users.


<?php

$country_code = $_SERVER["HTTP_CF_IPCOUNTRY"];

if ($country_code=="UK") {
$link = 'Insert UK Link';
}
elseif ($country_code=="FR") {
$link = 'Insert France Link';
}
elseif ($country_code=="DE") {
$link = 'Insert Germany Link';
}
else {
$link = 'Insert Default Link';
}

header("location:$link");
exit;

?>

A word of warning, for some users in the UK their country code is returned as UK and others are GB so you might want an extra check for both codes.

I was already redirecting people through this link to help manage my links, this just adds an extra level to that redirect. It has already paid off making sure that users from the countries I specified were redirected to the correct website for them. I currently have this redirect installed on a shared server with the website added to Cloudflare.

If you need to know specific country codes Wikipedia has a list that you can use.

This is a pretty simple method and Cloudflare takes care of maintaining the IP Geolocation database for you. Of course you could do more sophisticated things than this but this gets the job done for me.

LGR Photo Discontinued

Many years ago I wrote a quick PHP script to help me create a simple HTML photo gallery from JPG images. I released it as LGR Photo and gave it away for free. It was a great little gallery and did what I needed it to do. I made it mainly for my own purposes but many others downloaded it and found it useful. I have been wanting to rewrite the script for quite sometime now, but it never seemed to make it onto the list of things to do. That being said I think it is time to simply retire LGR Photo.

If people are interested in using the script I will offer it here still for download. You are welcome to use it, rewrite it, play with it etc, but I can no longer offer support for it.

Download LGR Photo

If you want to know more about the script here is some information that I had up about the script.

Features

LGR Photo Gallery is a photo gallery script written in PHP. The current version has the following features:

  • Easy Installation. A basic install on LGR Photo using the defaults should only take as long as it takes you to download it, unzip it, and upload it and some photos to your web server.
  • Uses the GD Libraries to dynamically create thumbnails from your photos.
  • Reads EXIF comment information from photos and displays the comment under the photo.
  • Next and Previous thumbnail images when browsing photos.
  • Table columns and rows adjustable.
  • Cascading Style Sheet used for presentation.
  • Easy to insert your custom header and footer files for complete web site integration.
  • Easy to insert into an existing web site. Allows for many different layout configurations.
  • Can use Javascript to pop open photos in a new window.
  • Creates albums from folder structure. Number of sub albums only limited by the operating system.
  • Not bloatware! Only two files for a quick and easy install. Perfect for home networks and to share photos with friends and family over the internet.
  • Simple slideshow component included with a basic install.
  • Valid XHTML and CSS in default install.
  • FREE! What more can you ask for.

Requirements

The following are the requirements to run LGR Photo on your server/computer.

  • A web server that supports PHP.
  • Any operating system that can run a web server and PHP.
  • PHP Version 4.3.3 and up.
  • GD Libraries are required to create thumbnails.
  • EXIF support enabled if you want to display EXIF comment information.
  • Currently only supports jpg files.

To make LGR Photo blend in more with your web site you will need to have some knowledge of:

  • XHTML
  • Cascading Style Sheets (CSS)
  • Some time to sit and play with it.

Include, Include_once, Require, Require_once

I have been working with several new clients lately, mostly helping them update their websites with new content. Most of these websites were created by other companies or individuals and I am always amazed at how people create websites. One of the things that I am always amazed at is why people don’t take advantage of using includes to make managing a website easier. Individual web pages in a website are made up of many common elements: headers, menus, footers. There is no reason to duplicate these items across the website when a single file can be included on all the pages to make management of the website easier. Most web servers have PHP installed on them, so may use of it to make managing your website easier.

The PHP include() and include_once() functions will allow you to include those common elements in all pages. If you have a common menu you can use a simple include such as:
<?php
include('menu.php');
?>

You might have to change your path. For example I like to keep all the include files in a separate folder so my includes will often look something like this:
<?php
include($_SERVER['DOCUMENT_ROOT'].'/includes/menu.php');
?>

To edit the menu and make the change across the entire website all I need to do is edit the one file menu.php. This can save you a considerable amount of time, especially if you have hundreds or thousands of web pages to edit.

The PHP functions that are most often used to include files are (from the PHP Documentation):

Include
The include() statement includes and evaluates the specified file.

Include Once
The include_once() statement includes and evaluates the specified file during the execution of the script. This is a behavior similar to the include() statement, with the only difference being that if the code from a file has already been included, it will not be included again. As the name suggests, it will be included just once.

Require
require() and include() are identical in every way except how they handle failure. They both produce a Warning, but require() results in a Fatal Error. In other words, don’t hesitate to use require() if you want a missing file to halt processing of the page.

Require Once
The require_once() statement includes and evaluates the specified file during the execution of the script. This is a behavior similar to the require() statement, with the only difference being that if the code from a file has already been included, it will not be included again.

When to use include() or require() can be an entire other post, but generally I use require() when the website needs that file to run. Site wide configuration files for example. If the file cannot be included it will stop the site from running. I use include() when it is less critical, so menus, headers, footers. The website might not look great if the menu does not get included but it will still display the information to people.

How To Add a Custom Welcome Based on Referrer

I recently added a little snippet of PHP to Video Rambler to welcome people that were coming from StumbleUpon with a custom welcome. The website recently got Stumbled again and one of the people that gave the site a thumbs up stated:

I only gave it thumbs up bc I’m wondering how they put that stumbleupon welcome message at the top…only shows if you stumble it.

Here is how I did it. I check the $_SERVER[‘HTTP_REFERER’] to see if it is one of the referrers I want to give a special welcome to. If it is then I simply echo some css to add some padding to the top of the screen and print out the custom welcome. This might not be fool proof, since some browsers do not pass the referrer, or people could fake the referrer, but it works quite well for StumbleUpon. You could make this more complicated and display a different message for more than just Stumblers, but those were the only people I was interested in at this time. I have been using it for a month and considering how well the website has been doing with StumbleUpon I think I will keep using it for a little while.

<?php
$allowed = Array('www.stumbleupon.com','stumbleupon.com');
$camefrom = array_change_key_case(parse_url($_SERVER['HTTP_REFERER']));
if (in_array($camefrom['host'],$allowed)) {
echo '<style type="text/css">body { padding-top: 25px; } </style>';
echo '<div style="position: absolute; top: 0; left: 0px; width: 100%; padding: 3px 15px 3px 15px; background-color:#ffffe1;">Welcome Stumbler! Hope you enjoy the site and it would be great if you would give a nice <a href="javascript:document.location.href = \'http://www.stumbleupon.com/submit?url=\'+ document.URL +\'&title = \'+document.title.replace(/%20/g,\'+\');">Thumbs Up!</a> Enjoy!</div>';
}
?>

Blogger Comment Link Love

One of the things that has frustrated me about Blogger, and Google to some extent, is the fact that they try to dictate to me how the links should be on my blog. For example comment links are automatically given the “nofollow” attribute. Well I don’t like that. I want the people who take the time to comment to get a link back to their website or blog. That seems fair. I have comment moderation in place so comments have to be approved before being published so I can delete the spammers comments and they won’t get any link love.

I searched through the Blogger help, which is pretty much useless to tell you the honest truth. Then I searched through the Blogger help group, and there were some answers there, but they were all for the new layout system when your blog is hosted on Blogspot. Well that did not help me so I went back to the Blogger help looking for the classic template tags and it was there that I got my idea.

The Blogger template tag <$BlogCommentAuthor$> is the template tag responsible for printing out the comment authors name and link. There is no way of telling it not to output a rel=”nofollow”, so enter in a little snippet of PHP to modify that little bit of HTML that Blogger is inserting into my blog. Look where Blogger has the <$BlogCommentAuthor$> template tag replace it with <?php echo str_replace(' rel="nofollow"', '', '<$BlogCommentAuthor$>'); ?> and presto that nasty little rel=”nofollow” attribute is gone. Of course this only works if you are processing your HTML files as PHP.

On some blog templates you might not find the <$BlogCommentAuthor$> template tag, instead they have <$I18NPostedByCommentAuthor$>. It does exactly the same thing. Just replace that tag with <?php echo str_replace(' rel="nofollow"', '', '<$I18NPostedByCommentAuthor$>'); ?> and let the link love begin.

On a related note, if you leave a comment and want to have a link pointing to your website make sure you choose the “Other” under “Choose an identity”. This way you can specify your website or blog address instead of a Blogger profile. No one needs to have link love to their Blogger profile.

Oh and what is the cost for all of this link love? Unlike some bloggers out there who have decided to charge $10 to remove the nofollow attribute, I will charge you ABSOLUTELY NOTHING! In case you are wondering the above link is a nofollow, because he does not need anymore links.

Speed Up Your Website with GZIP Compression

I love little tricks that make a huge difference to a website. This addition can cut your bandwidth, possibly by as much as half, and it will make your pages load faster for those on slower connections and all you have to do is enable GZIP compression.

There is a great post over at BetterExplained that goes into more depth about how to implement GZIP compression if you are using Apache 2 and it is worth reading.

A lot of hosts don’t run Apache 2 on production servers though, and I know that my servers are not running Apache 2. You can get the same benefits by adding some very simple PHP code to your web pages.

<?php
if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')) {
ob_start("ob_gzhandler");
}
else {
ob_start();
}
?>

Now if you build your website with templates and PHP includes for headers and footers, you will only have to add this PHP code to one file to start delivering your web pages with GZIP compression. I have started using this on both this blog and Video Rambler and the compressed pages are often half the size of the uncompressed page. Here is a screenshot of the index page document size.

Notice in the screenshot of the document size that the majority of the document size is images, mostly from MyBlogLog. Gives me reason to rethink whether or not I should keep it on the site.

Blogger Label List for FTP Published Blogs

I use Blogger for this blog, mainly because it is easy to use. One of the features of the new Blogger that I like is the ability to add labels to posts, but because I publish via FTP there is no way to use the labels widget that is available to users of the new Blogger that publish to Blogspot blogs. Well I really wanted to be able to list all the labels I use so there is easy access to posts by categories so I came up with a quick little PHP script to do just that.

First you have to know that when the new Blogger publishes the labels to a server via FTP it creates a folder called labels and publishes all the label pages to that folder. It also makes the file names the same as the label. This makes it easy to create a script to read all the file names in the labels folder, and output the names.

Here is a copy of the PHP script that you can use to read all of the .html files in the labels folder and output them as an unordered list. Save it and place it in the labels folder of your web server. In the script it does not include the php script in the list, if you save the script with a different name than “labelslist.php” then make sure you change filename in the script.

Update: Download the code for the labels list instead of copying and pasting. Download

<?php

$labelsdir=$_SERVER["DOCUMENT_ROOT"].'/labels';

$files=array();

if ($handle = opendir($labelsdir)) {
$fkey=0;
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != ".." && $file != "index.html" && $file != "labelslist.php" && ereg(".html$",$file)) {
$files[$fkey]['name'] = $file; $fkey++;
}
}
closedir($handle);
}

sort($files);
echo '<ul>';
foreach ($files as $key=>$value) {
echo '<li><a href="/labels/'.$files[$key]['name'].'">'.ucwords(substr($files[$key]['name'], 0, -5)).'</a></li> ';
}
echo '</ul>';

?>

Now if you have setup your server to process .html files as php then your server will process PHP includes that you have in your Blogger template file. Find the location in your Blogger template that you want to include the labels list and do a simple PHP include

<?php include_once($_SERVER['DOCUMENT_ROOT']."/labels/labelslist.php"); ?>

and republish. Your label list should now appear on your FTP published Blogger blog.

The script outputs an unordered list so you might want to style the list so it is similar to the rest of your blog. It is also possible to make this list look more like a tag cloud. If people want to know how to do that leave a comment and I will add another post to help create a tag cloud style list. Happy blogging!

EDIT: Just noticed that when Blogger creates the label pages that have spaces in the names it is properly encoding them with a %20 as the space. To stop the %20 from showing up and actually have a space modify the line in the code from this:

echo '<li><a href="/labels/'.$files[$key]['name'].'">'.ucwords(substr($files[$key]['name'], 0, -5)).'</a></li> ';

to

echo '<li><a href="/labels/'.$files[$key]['name'].'">'.ucwords(substr(urldecode($files[$key]['name']), 0, -5)).'</a></li> ';
That should decode the %20 so a space actually shows up on the link.

EDIT: The change Blogger did has turned into more of a pain in the neck than it should have been. Check out this post titled “Spaces in Blogger Labels – A Change” about why. Anyways, probably the best solution is to not use spaces in your labels but use a dash instead. You can then change the line of code so it looks like this:

echo '<li><a href="/labels/'.$files[$key]['name'].'">'.ucwords(substr(str_replace('-',' ',$files[$key]['name']), 0, -5)).'</a></li> ';

You would think that Blogger would know better, why didn’t they just use dashes in the label file names to begin with, that is what they do with the blog post filenames.