You are currently browsing all posts tagged with 'code tidbits' in the JHuskisson.com post archives.
-
Changing the default Mac screen shot image type
I recently had the need to change the default Mac OSX takes it’s screen shots in. There’s some occasions where you need more quality than PNG and some occasions where you need a file anyone can open and simply don’t want PNG as your default.
Well, in OSX 10.4 and onwards you can change the default screen shot format.
Start by opening Terminal, located in /Applications/Utilities/Terminal
Type in:
defaults write com.apple.screencapture type image_format
killall SystemUIServerReplace image_format with your selected format that you wish to change the default setting to. The second line reloads the default so that you don’t need to log out and back in again for the change to take effect. The next time you take a screen shot after executing both of these commands, it will be saved in the new format you have chosen.
Below is a list of several popular image types and their commands in full should you wish to paste them.
BMP
defaults write com.apple.screencapture type bmp
killall SystemUIServerGIF
defaults write com.apple.screencapture type gif
killall SystemUIServerJPG
defaults write com.apple.screencapture type jpg
killall SystemUIServerPDF
defaults write com.apple.screencapture type pdf
killall SystemUIServerPNG
defaults write com.apple.screencapture type png
killall SystemUIServerTIFF
4 Comments »defaults write com.apple.screencapture type tiff
killall SystemUIServer -
Adding an unsubscribe link to a Magento newsletter template

A quick tip for Magento developer’s here, in how to add an unsubscribe link into a newsletter template so that the person can unsubscribe from the newsletter if they chose to do so.
Simply add {{var subscriber.getUnsubscriptionLink()}} into your template in order to output the link for the user to unsubscribe. This even works for people who aren’t registered customers of the Magento installation but that you’ve imported into your system as subscribing to the newsletter.
Full example of a link in your template to unsubscribe:
6 Comments »<a href=”{{var subscriber.getUnsubscriptionLink()}}” title=”Click here to unsubscribe from this newsletter”>Unsubscribe</a>
-
‘Share on Twitter’ link
Ever wanted to give your readers, or your client’s readers a chance to share the post quickly on Twitter?
The code is straight forward:
<a href=”http://twitter.com/home?status=Currently reading http://www.test.com/post-url” title=”Click to share this post on Twitter”>Share on Twitter</a>
If you’re using Wordpress and want to automate this, simply use the following to insert the link to the current post in the loop into your link:
53 Comments »<a href=”http://twitter.com/home?status=Currently reading <?php the_permalink(); ?>” title=”Click to share this post on Twitter”>Share on Twitter</a>
-
301 and 302 redirects with .htaccess
I’ve been asked this question time and time again, so here we are. How to redirect directories and old URL’s to new ones when moving your site from one domain to another, or launching a new version with re-structured URLs.
Here’s a sample structure of a re-direct line:
Redirect 301 /about.php http://www.newdomain.com/about
/about.php here is the local URL relative to the core domain you’re redirecting from. If you’re redirecting from http://www.sample.com/directory/about.php then this will be /directory/about.php and if you are redirecting from http://www.sample.com/about.php then it is simply /about.php.
The second part of this statement is the complete URL you are forwarding to.
The beauty of a 301 redirect with .htaccess is that if you have one big set of URL’s that have changed from one place to another with the filename/end URL staying the same it is still the same statement. For example:
Redirect 301 /test/ http://www.newdomain.com/testing/
This redirects /test/about.php to http://www.newdomain.com/testing/about.php – anything after /test/ will be redirected to http://www.newdomain.com/testing/ and placed at the end of the URL. The same applies for all query URLs (i.e. about.php?variable=1&this=that).
Tip: Always priority order your redirects. The redirects you want done first should go up top. But you should also be careful not to place inherent statements above specific ones.
This won’t work:
Redirect 301 /test/ http://www.newdomain.com/testing/
Redirect 301 /test/about.php http://www.newdomain.com/about.phpThis will result in the first statement being processed and the second statement meaning nothing. The correct order is:
Redirect 301 /test/about.php http://www.newdomain.com/about.php
Redirect 301 /test/ http://www.newdomain.com/testing/This means the specific statement is applied first, and then the inherent statement is ran afterwards.
Here’s a sample of our extensive re-directs for Sausage Roll to Rolled:
Redirect 301 /general-posts/ http://www.rolled.at/general-news/
Redirect 301 /wp-content/uploads/11/ http://www.rolled.at/wp-content/uploads/Here is what they do:
- Moves all posts in the /general-posts/ slugged category to show at /general-news/ on the new site. For example /general-posts/testing-post is now /general-news/testing-post on the new site as we changed the category.
- All uploads from /wp-content/uploads/11/ are now linked to in /wp-content/uploads/. For examples /wp-content/uploads/11/test.jpg is now /wp-content/uploads/test.jpg on the new server.
All of these lines can be applied to 302 (temporary) redirects too. Just switch the number 301 with 302 to achieve the result you need. 302 redirects should be used where the file is only moving there for a short while but will be put back eventually and 301 redirects are used to transfer the URL to the new URL permanently and work great for a site move or a URL restructuring.
I used these to transfer Twod.co.uk to JHuskisson.com and several other transfers and they have all retained by page rank and search engine strength from domain to domain with ease.
Have any other htaccess redirect tips or things to discuss? Feel free to leave a comment using the form below.
3 Comments » -
Force www. in your URL using .htaccess
Insert this into the top of your .htaccess file, making sure that ‘RewriteEngine On’ comes before it:
RewriteCond %{HTTP_HOST} !^www\.jhuskisson\.com [NC]
RewriteRule ^(.*)$ http://www.jhuskisson.com/$1 [R=301,L]Replace jhuskisson.com with your chosen domain and make sure if you are placing any full stops within the first line to enter a backslash before each full stop. This will prevent the code from not working.
9 Comments » -
‘Send me a message on AIM’ link

Here’s the code:
<a href=”aim:goim?screename=username&message=hello”>Send me a message on AIM</a>
Replace username with your required AIM username and ‘hello’ with whatever message you wish to set for the user to send you. You can remove ‘hello’ altogether if you don’t wish to pre-set a message for a user to send you upon clicking the link.
3 Comments » -
Re-enabling error reporting in MAMP
For some reason error reporting is turned off by default in MAMP.. So this is how to re-enable it to how it should be.
1. Find your ‘MAMP’ directory in ‘Applications’
2. Find ‘conf’ and open it up..
3. Open up php4 or php5 (depending on your enabled version)
4. Line 270 should be something like this:
error_reporting = E_ALL5. Line 277 will be:
display_errors = OffChange this to:
display_errors = On6. Restart servers
And voila! We have PHP errors..
39 Comments » -
Track outbound links for Google Analytics automatically
Place this code before the </body> tag and it’ll track all outbound links that aren’t part of your website.
<script type=”text/javascript”>
if (document.getElementsByTagName) {
var ahrefs = document.getElementsByTagName(‘a’);
for (var i=0; i<ahrefs.length;i++) {
if (ahrefs[i].href.indexOf(‘http://www.jhuskisson.com‘) == -1 && !ahrefs[i].onclick) {
ahrefs[i].onclick = function () { var track = this.href + ”; urchinTracker (‘/outgoing/’+track.substring(7)); }
}
}
}
</script>Simply replace http://www.jhuskisson.com with your own site URL and hey presto – all outbound links that aren’t internal are being tracked by Google Analytics.
22 Comments » -
From PHP 4 to PHP 5.. PHP Fatal zlib error: imagepng()
Getting this error?
PHP Fatal error: imagepng() [function.imagepng]: gd-png: fatal libpng error: zlib error in example.php on line 10
Fix it easily by changing your compression variable of imagepng, imagegif or imagejpg/imagejpeg into a 1-10 ranged number – instead of the PHP 4 1-100 standard.
It should work smoothly now :)
12 Comments » -
Never lose visitors from old URLs again
Was coding a script for redirection of old URL’s into new ones for Wordpress when I found this plugin.
This plugin redirects the old ?p=1 ?page_id=1 URL structure as well as handling feedburner re-direction. Which saved me a lot of time and frustration. It also means converting sites not using permalinks into sites with permalink structures is a simple case of enabling this plugin. I love it!
No Comments »


