Archive for the ‘Tutorials’ category

How to integrate Mandrill into your PHP site

May 18th, 2013

You might have heard of Mailchimp… who hasn’t these days! Well the same guys have released a rather nice little system called Mandrill (or did earlier this year at least). It is a transactional SMTP relay service with a rather nice little API for those who care.

For those that don’t give a monkeys (get it.. monkeys?!) I have a barebones little bit of PHP code for you to use which will effect the sending of your emails. Most sites using PHP have a central ‘mail’ command. There will be a function in a file or include somewhere which will handle the sending of mail using this sort of syntax:

mail($to, $subject, $body);

Quite simple.. it’s how PHP sends emails normally. The following code is to replace that mail command. Don’t forget to enter your API key!

$to = 'test@test.com';
$content = '<p>this is the emails html <a href="www.google.co.uk">content</a></p>';
$subject = 'this is the subject';
$from = 'test@mandrill.com';

$uri = 'https://mandrillapp.com/api/1.0/messages/send.json';
$api_key = 'YOUR KEY HERE';
$content_text = strip_tags($content);

$postString = '{
"key": "' . $api_key . '",
"message": {
 "html": "' . $content . '",
 "text": "' . $content_text . '",
 "subject": "' . $subject . '",
 "from_email": "' . $from . '",
 "from_name": "' . $from . '",
 "to": [
 {
 "email": "' . $to . '",
 "name": "' . $to . '"
 }
 ],
 "track_opens": true,
 "track_clicks": true,
 "auto_text": true,
 "url_strip_qs": true,
 "preserve_recipients": true
},
"async": false
}';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $uri);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postString);
$result = curl_exec($ch);

You can also echo the $result variable to debug this code.. it will return a string of JSON with either a success message or an error. Rather useful I think!

WordPress page header images made easy with SB Uploader

March 22nd, 2013

Screen Shot 2013-03-22 at 11.55.13A couple of years ago I wrote a plugin called SB Uploader. I did this because I didn’t like the media library that WordPress used and wanted to setting the featured image within any WordPress post or page simple. Well after a couple of years of development it is now in use by potentially thousands of people (perhaps a tad self indulgent but it has been downloaded a lot and is bundled with a fair few stock and premium themes) and supports custom post types and taxonomies as well as being able to be added in multiples (several configurable upload boxes per post type).

I use it with practically every theme I write for people and it is by far my most popular plugin yet (alongside SB Welcome Email Editor). I wanted to show people how you would set up configurable header images for pages and maybe posts using the script.

There is a function called sbu_the_wp_image which will return the featured image for the current post item (page or custom post type too). You pass it the size you want it to return (thumb, full or a custom image size) and there you have it. Job done. Just add the code to your page.php file above the call to the_content() (or wherever makes sense in the markup) and it will work.

<?php if (function_exists('sbu_the_wp_image')) { echo sbu_the_wp_image('feature_header'); } ?>

The above code is all you need to call the featured image for the page and the following code should sit in your functions.php file or be configured via a plugin in order to tell WordPress what feature_header actually relates to in terms of image size.

<?php add_image_size('feature_header', 960, 200, true); ?>

It defines the image size, sets the dimension to 960×200 and the ‘true’ portion tells it to hard crop which will guarantee that it is always that dimension by cropping after the initial resize rather than using a proportionate resize and providing an image within those limits.

You can get SB Uploader from the WordPress repository. Any ratings or feedback is welcome as are any feature requests. I hope to be able to write something to better deal with WordPress galleries before long as I am not satisfied with the way they work at the moment. Watch this space!

How to remove the WooCommerce 2.0+ Reviews tab

March 7th, 2013

woothemes-ninja3On the 6th of March (I think) the lovely guys at WooThemes graced us with a lovely new version of their fantastic WooCommerce plugin. They changed a lot of things, most of which I have yet to absorb. A couple of notable things though caught my eye. You may or may not be aware that I have written a few tutorials concerning the WooCommerce tab system. Adding new ones and, of course, removing the reviews tab. Since 2.0 the product tab API has been updated and is now somewhat slightly more elegant. » Read more: How to remove the WooCommerce 2.0+ Reviews tab

Help! Help! My [X] doesn’t work! – How to sort out basic WordPress issues and getting to the cause of a problem without the need to panic

January 17th, 2013

broken-linkHappy new year! About this time of year clients come out of the woodwork after a lovely Christmas and with a renewed enthusiasm in their business and sites. So the first thing they do is mush the big Upgrade button within WordPress and upgrade their site core, theme and/or plugins. And then what happens? Well… nothing as it turns out because inevitably the last time they upgraded was a year ago and someone else did it and he’s gone on sabbatical to Jupiter and taken all of the backups with him and the company who wrote the site have gone bust and you might have been hacked a few times since then but a bloke from the pub sorted it out.. and so on.

So when you have the white screen of death (WSOD) or PHP errors all over the site what do you do? Well..

» Read more: Help! Help! My [X] doesn’t work! – How to sort out basic WordPress issues and getting to the cause of a problem without the need to panic

Adding your own WooCommerce Tabs simplified step by step

November 5th, 2012

woocommerce_logoMy original tutorial on WooCommerce product tab addition has gone down very well. However it has been brought to my attention that people who don’t know much PHP are struggling to follow it. This is ok.. we all need to start somewhere but for convenience I have put it all into a plugin and commented each line with instructions accordingly. The content of that plugin is directly pasted below with a download link at the bottom for you. It will work out of the box with no modification. You just need to add a custom field to your products which have additional information. If anyone is interested I shall write another tutorial on how to add a nice WYSIWYG editor to the product admin pages for that more integrated feel.

» Read more: Adding your own WooCommerce Tabs simplified step by step

Adding your own currency to WooCommerce

October 31st, 2012

woocommerce_logoI have been asked on a few occasions by people how to add a new currency to WooCommerce. It’s actually incredibly simple to do thanks to the excellent WooCommerce API. Simply add the following code to your theme functions.php file or to a plugin of your own for this to work. I used this same code in my recent Interswitch Payment Gateway integration for WooCommerce to add Nigerian Naira.

» Read more: Adding your own currency to WooCommerce

Costs of a basic website and how to go about getting one

October 17th, 2012

Piles-of-MoneyThis is something that people seem to be searching for so I thought I should write a post to explain. It seems that the general knowledge of websites and their costs is fairly low and therefore I shall explain.

There are four main costs when setting up a new website. These are Domain, Hosting, Design and Programming. I shall talk about each area in some detail.

Domain

The domain is the name of your site. My site is called currently sean-barton.co.uk but I intend to move it to tortoise-it.co.uk very soon. A domain is essentially a lookup service which turns  a IP address (a set of numbers to uniquely identify the server on which your website resides) into a name such as tortoise-it.co.uk. This is 100% necessary for you to buy. People charge varying amounts for domains and the cost will fluctuate depending on both the person registering it for you (the design agency or technical contact you are dealing with) and the domain registrar, the company which actually sells the domain to you and provides you with an interface for administration. My recommendation is to always register the domain on your own or with advice but perhaps not doing it via a technical person such as myself as it cuts out both complication in billing and of course overall control of the brand linked domain. I personally use 123reg.co.uk who charge £2-£5 for a .co.uk domain I think although, of course, there are a vast multitude of registrars around the internet. Once you buy your domain I recommend putting it onto direct debit or a card which you don’t think will expire any time soon because one of the most common causes of older sites going down is the owner forgetting to pay up each year for the name.

Hosting

I won’t go into too much detail here but I shall say that hosting is a massive area of the internet. Costs are annual although a lot of companies offer a pay monthly thing and normally accepting payments via Paypal. If you don’t have a Paypal account then get one as they are both free and ubiquitous in western society. I personally use Hostgator. use ‘tortoise25‘ in the coupon field for a discount. Yes I get a kick back but you also get a cheaper deal than their public prices, even with any seasonal sale they might have on. So what level of server do you need? They are so complicated right? Well 99% of the people on the internal will only need the cheapest deal that hostgator will offer for $8 per month. Forget that the price is in dollars as Paypal will do the conversion and charge you accordingly. However running a server isn’t entirely straightforward and therefore your technical contact or agency might offer to host on your behalf and bill you accordingly each year. This is generally a bit more expensive but it means you get more value and things like email management, crisis recovery and backups are handled accordingly. This is even more important if your livelihood is reliant on your website being up. A general price for a ‘managed’ (by someone like myself) hosting package capable of running any type of site with no imposed limits or quotas is around £100-£150 per year. It is not entirely important for you to have control of this as servers are interchangeable unlike domain names.

Programming

So you have somewhere to put your site. Now what?..

A website is written in HTML, the language of the web. You might have heard of it.. it’s been around long enough. However there are languages which people like myself use to generate the HTML of a site. CSS for styling, JS and jQuery for moving and dynamic elements and then on top of that all you have two of the most popular languages of the web PHP and ASP. The latter, ASP is antiquated and used only by those people who don’t know better. Support for it is low and software for websites written using it is generally horrendously and unnecessarily expensive. However.. PHP on the other hand (you can tell which I use by now I hope).. This is all largely irrelevant  but you need to be aware of the language names at least. ASP generally runs on a Windows server and PHP on a UNIX server. They are, however, interchangeable but they are the normal configurations. There is also SQL, the language of the databases of the internet. This comes in two flavours MySQL and MSQL for PHP and ASP respectively. All of these languages are free to use although ASP sites are generally both more expensive to develop and maintain on account of ASP developers asking a premium for their efforts in my experience.

Deciding what you want your site to accomplish will largely effect the cost. Right now I shall say anyone that charges by the page just walk away. Websites are not written by the page and therefore charging for a 3 page site differently to a 5 page site is misguided. The amount of effort that goes into a site is magnified with each template (or unique layout). You might find that your 50 page site includes a unique homepage layout and then 49 pages which share a common layout. To a developer such as myself we consider this to be two pages of effort with varying content.

There are a few types of site I generally meet… magazine/news, brochureware, ecommerce and bespoke. For the vast majority of sites we can use a base system called a CMS or Content Management System which puts a prebuilt user interface and administration system in place for someone like myself to ‘theme’ and extend with ‘plugins’. Those who claim to ‘hand code’ sites clearly know no better. Unless of course they don’t mean they use ‘static code’ and they mean that they don’t use a piece of software like Adobe Dreamweaver to generate their sites for them. It’s not a bad thing so much as it not being very effective and the results generally being fairly poor.

The CMS’ of choice on the internet right now are WordPress, Joomla and Drupal. The latter two being hopeless and outdated so I tend to use WordPress unless the client specifies otherwise in which case I convince them. WordPress is actually powering a vast corner of the internet at the moment. The range of plugins and themes available to allow a non technical person to put a site together is phenomenal. I got into WordPress a few years ago and have since then been making it do some very complex and very useful things for my clients. All the while at the end of the job they remain in full control of their sites and do not need to call me each time they want to change some text, add a page or upload something. All three CMS’ are free to use and there are both free and premium extensions for them.

WordPress has their own directory of ‘themes’. A theme is a set of files and images which can be applied to a WordPress site to give it the look and feel that you want. There is a plethora of free themes on the internet but mostly they are amateur efforts in both code and end result in my experience. Of course also using a free theme for your site means you don’t have that unique or branded feel to it. Themes come in varying shapes and sizes and of course qualities. Of course the ‘free’ aspect of this attracts people and so is an option. I’m not suggesting that all free themes are bad but the compatibility and support level will always be unknown to you. If you would like to try and make a site yourself then I recommend getting a premium theme. You need to pay for them obviously but the support will be better and the quality of work and compatibility is likely to be superior. I tend to recommend Woothemes as a premium theme supplier. They charge $50-$70 for their work or you can buy an annual thing for a couple of hundred and get access to all 97 or so themes they offer.

For those that do definitely want something unique for them the only path is to use a graphic designer. People like myself (developers) have good relationships with these sorts of people as we often work closely together. The job of a graphic designer is to take your vision (or help you build one) and turn it into a site design for someone like myself to then take and turn into a theme/site combination. Generally a designer will want to talk to you about your existing branding, logos, fonts and colour schemes and generally ask for examples of sites or chunks of sites you like with reasons/justification. They will then produce a set of ‘concepts’ for you to look at and then you would approve one before it goes to full design. Costs vary per designer. In my experience they generally ask for a similar amount of money to a developer such as myself although there is a huge range of prices.

A developer or technical person such as myself will be able to guide you through the entire process. You could talk to an agency who will be able to provide the full service from hosting for you to design and development and of course maintenance. A site generally if a design is done or a template being used should cost less than the average persons monthly wage or even less if ou specify you are on a budget. However what most people tend not to realise is that not wanting to spend a lot directly effects the end result. You can’t walk into a Ferrari dealership with £10,000 and leave with more than a brochure. It’s the same in this business. Frankly you get what you pay for although people in my profession will always be able to talk you through the options and find the right balance of cost to quality. Perhaps I shouldn’t use the word quality. It’s more the functionality you can have which might need to change. Designers, in my experience, tend not to charge more for more features.. only more time (which of course has a cost).

Ecommerce

Having a shop on your site isn’t as complex as you think. Yes it increases the cost by a not insignificant amount but using WordPress and a plugin called Woocommerce (which is free) you can have basic shop functionality without the need to rebuild your existing site. The thing which does cost money though is Payment Gateway integration. If you want to use your bank rather than Paypal and if there isn’t a prewritten module for it then you would need to have the interface written. This varies in cost dependant on who it is. The more popular ones are available as premium addons for prices ranging between £20 and £100. I tell my clients that a basic gateway at a guess will cost around £200 to integrate. Then depending on the level of documentation and support available work can commence. You might be tempted to go for a standalone shopping cart for your site and have a blog or content pages separately but this is only really recommended when you are going to have thousands of products and want a quite complex shop setup. Most people do not need this and the content capability and extensibility of a CMS such as WordPress should make up your mind in it’s favour. If you want to shop around (bad pun) for ecommerce platforms there are several free ones in use, the most popular being Magento but some older programmers using OSCommerce or Zencart. I recommend you stay away from these last two as they are nothing but trouble.

Bespoke

Sites which perform a complex action or have a purpose other than to show of your hobby/business or sell your products might be classed as a bespoke build. The above comments can mostly be thrown out in this case as each project is different and therefore each cost is going to be different. Either way it won’t hurt talking to someone with contacts and to help you flesh out your ideas.

Shameless self promotion

What sort of businessman would I be without the obligatory contact form at the bottom. You can get in touch using this form and I shall normally reply within a few hours if only to acknowledge receipt of your contact. I do not charge for quoting people and will be able to give a ballpark figure for some work based on a short conversation.

Your Name (required)

Your Email (required)

Contact Number (if relevant)

Where did you hear about me?

Where are you based?

Your Message

WordPress default featured image

October 3rd, 2012

I have often wondered what to do in WordPress if no featured image is used for a page or post. Normally I just use a getter and if it’s empty then I will use an image defined in the template. This is ok for new sites although not as efficient as this method which uses a WordPress filter to check for the existence of the image and if it doesn’t exist will return the placeholder instead.

This way the code is central and can be changed easily. What it doesn’t yet do is differentiate the size of the image by the size name and return the appropriate sized placeholder. Should be easy enough to extend though:

add_filter('post_thumbnail_html', 'sb_post_thumbnail_html', 100, 5);
function sb_post_thumbnail_html($html, $post_id, $post_thumbnail_id, $size, $attr) {
 if (!$html) {
 $html = '<img width="150" height="150" src="http://placehold.it/150x150" class="attachment-thumbnail wp-post-image" />';
 }
 
 return $html;
}