Code Snippets Blogs

03
May

Configuring GeoIP extensions for PHP

PECL provides extensions for PHP, including GeoIP. When GeoIP is installed you gain access to powerful, simple to use functions used to determine geographical locations of IP addresses and domain names. This is useful if you want to set a language or currency based on the country of visitors to a website, for instance. Check out the php docs for a full list of GeoIP functions, and read on to find out how to rock it on your site.

21
Apr

jQuery plugin to Dynamically shorten HTML lists

Sometimes lists can be just way too long. However, you can use the following script to easily shorten lists with a "List more" button using this jQuery plugin.

29
Mar

How do I override controller actions in Magento 1.4?

From experience, everyone knows overriding Magento core functionality can be deadly if not done right. This tutorial gives an example of how to cleanly override core controllers. I’ll use the example of overriding the core contact form’s post-submit action to redirect to a custom thankyou page.

Step 1

First, we need to tell Magento that we have a new module by adding an XML file at /app/etc/module/Dhmedia_Contacts.xml with the following contents: <?xml version="1.0"?><config> <modules> <dhmedia_contacts> <active>true</active> <codepool>local</codepool> <depends> <mage_contacts> </mage_contacts></depends> <version>0.1.0</version> </dhmedia_contacts> </modules> </config> This lets Magento know there is a module in the “local” code pool called Contacts in the Dhmedia namespace (/app/local/Dhmedia/Contacts/...)

Step 2

So now we've told Magento to look for our module, and it does this by trying to find the config.xml file at /app/code/local/Dhmedia/Contacts/etc/config.xml. Create that file with the following contents: <?xml version="1.0"?><config> <modules> <dhmedia_contacts> <version>0.1.0</version> </dhmedia_contacts> </modules> <frontend> <routers> <contacts> <args> <modules> <dhmedia_contacts before="Mage_Contacts">Dhmedia_Contacts</dhmedia_contacts> </modules> </args> </contacts> </routers> </frontend> </config> This file tells Magento to load Dhmedia_Contacts before the core Mage_Contacts. Now any action overrides in our local module will take priority over the core functionality.

Step 3

Now that Magento knows where to find our module and which modules we're rewriting, we need to provide new behavior. In this example, we override the default contact post action by extending Mage_Contacts_IndexController with our own class, Dhmedia_Contacts_IndexController. Do this by creating /app/code/local/Dhmedia/Contacts/controllers/IndexController.php with the following contents: <?php
include_once "Mage/Contacts/controllers/IndexController.php";

class Dhmedia_Contacts_IndexController extends Mage_Contacts_IndexController
{   
    public function postAction()
    {
        parent::postAction();
        $this->_redirect('thankyou');
    }
}
?>If you can follow the flow of execution, Magento will call our postAction method when a form is submitted. Our method calls the parent’s post action to invoke the core behavior so we don’t need to copy any core code into our module. Next we tell Magento to redirect to the ‘thankyou’ page. Assuming you have created a CMS page in the admin backend with ‘thankyou’ as its URL alias, the task of overriding the default post redirect in an upgrade-proof way is complete.

The potential is limitless

You can use these same techniques to override pretty much any core Magento controller’s actions, or even to define your own actions. This is a very powerful technique and very poorly documented, so I hope this post has been of some use to somebody. This information should be correct as of version 1.4.1.1 and is hopefully good for another couple of major revisions.

23
Mar

Nice placeholders with jQuery!

Get rid of those ugly labels on your text forms and turn them into nice HTML5-style placeholders with this jQuery plugin. Full Drupal integration included, of course!

14
Jan

How to geocode an address using Google and php

Google has some amazing services, one of my favourites would have to be the ability to geocode an address and get not only the lat/long but a clean, well formatted address back. 

How you say... Easy!

Get yourself an api key from here: http://code.google.com/apis/maps/signup.html

use the following php code: