Change the title of 404 page in wordpress

In WordPress its very simple to create a 404 page, place a file with your design and name it as ‘404.php’. That’s it, very simple your custom 404 page is ready. By default, the title of the 404 page will be showing “Page not found”. If you want to customize that and change that and have your own custom title for the 404 page.

Then open the header.php, file which has the code to generate the html title. you can find the following line in the file.

wp_title( '|', true, 'right' )

Now replace the above line with

if( is_404() ) {
    echo '404 message goes here | ';
} else {
    wp_title( '|', true, 'right' );
}

And you are done, Now you can see your custom 404 error on the browser title bar.

Changing favicon in Magento

A favicon (short for favorites icon), also known as a shortcut icon, Web site icon, URL icon, or bookmark icon, is a file containing one or more small icons, most commonly 16×16 pixels, associated with a particular Website. In Magento we have a beautifulfavicon, that represents Magento logo. If you want your own favicon to be displayed on your site, you need to change the default one with the one that represents your site logo.

Generally, all the sites and CMS have their favicon in the root directory of the site, in Magento, it has favicon in six other places to displayed the icon different for each section. Lets say, If you want a different icon to be displayed in your frontend user section and other in the admin section, you have provision for that in Magento. 

You need to replace the favicon.ico in the following locations.

/{root_directory}

/errors/default/images

/skin/adminhtml/default

/skin/install/default

/skin/frontend/default

/downloader

If you are not confident of changing the files, Magento has a backend interface throught which you can change the favicon for the frontend site.

Login to the admin section and navigate to 'system -> configuration -> General -> Design'. Select the third tab saying 'HTML Head'. In the first field you can browser your local system and upload the favicon file to be displayed on your site.

Thank you for reading this article.

Adding Breadcrump to cms pages in Magento

 

Magento is a eCommerce application and it supports CMS features but it has few limitation. Magento displays breadcrumps, which helps the user to know where they are and it displays the navigation path. But this breadcrump is available only in the catalog pages an not in the cms pages or in the checkout pages. 

'breadcrump.phtml' in 'app/design/default/yourtemplate/page' folder is responsible for displaying the breadcrump. Add the following code to the breadcrump.phtml file before the normal breadcrump check  to display the breadcrump in the cms, shopping cart and checkout pages.

if ((!$crumbs || !is_array($crumbs)) && $this->getUrl('') != $this->getUrl('*/*/*', array('_current'=>true, '_use_rewrite'=>true))) {
 
    $breadcrumb = $this->getLayout()->getBlock('breadcrumbs');
 
    $breadcrumb->addCrumb('home', array('label' => Mage::helper('cms')->__('Home'), 'title' => Mage::helper('cms')->__('Home Page'), 'link' => Mage::getBaseUrl()));
 
    $breadcrumb->addCrumb('my_account activeBreadcrumb', array('label' => $this->getLayout()->getBlock('head')->getTitle(), 'title' => $this->getLayout()->getBlock('head')->getTitle(), 'last' => 1));
 
    $crumbs = $breadcrumb->_crumbs;
 
}

The above code restricts the breadcrump to be displayed from the home page, if you want that to be shown also in the home page, then remove the following condition from the first line of the above code.

$this->getUrl('') != $this->getUrl('*/*/*', array('_current'=>true, '_use_rewrite'=>true))

This code check whether the current page is home page or not. Since Home is a CMS page in Magento, normally breadcrumps won't be displayed. 

Debug magento with Zend_debug::dump

dump() is a static method defined in debug class under Zend. This one of the more stuffs I love in Magento. Because it helps me debug any variable or objects in simple ways. You can echo what ever you want, lets say, a simple variable, an array, an object, any complex data type and so on…

Syntax:

Zend_Debug::dump($var, $label = null, $echo = true);

The $var argument specifies the expression or variable about which the Zend_Debug::dump() method outputs information.

The $label parameter prepends the string provided before the output. Mostly we don’t use this parameter, but you can see its use when you dump values of same variable at different lines of code, that time only this $label field will help you to understand the corresponding results.

The $echo parameter is to make dump() function not to print the values if set to false. Regardless of the value of the $echo argument, the return value of this method contains the output.

In core php, the dump method is equivalent

echo “<pre>”;
var_dump();
echo “</pre>”;

 

But this function returns more formatted and readable result. Instead of writing three lines of code every time for debugging, we can now use a single line of code.  From now start using Zend_debug::dump() and your love towards Magento/Zend will be doubled for sure.

Working with Google Adwords in Sandbox mode

Google Adwords is a tool to create and manage your ads while the API helps you to do those stuff programmatically. Since Adwords is a paid service, Google provides a Sandbox environment to make our hands dirty by playing with code. To have a quick knowledge about adwords have a glance at my previous post about An introduction to Google Adwords. Adwords has API support in Java, Php, DotNet, Python, Perl, Ruby. Here we are going to look into the API development in PHP. The PHP client library for Adwords is available at Google Code.

Download and unzip the package to your server. Remember this API works only on SOAP enabled servers.

Inside the aw_api folder, you can find a ‘src’ folder, which carries the core files responsible for all the service calls. in the ‘example’ folder, there are pre defined working examples to create/add/delete campaigns, ads, ad groups, etc.,. Before working on that, we need to configure the package to work on sandbox.

For that going to the folder ‘src -> Google -> Api -> Ads -> AdWords’. There you can find two files, ‘auth.ini’ and ‘settings.ini’.

open the ‘Auth.ini’ file:

update the following few lines in that file

email = "<em>youremail</em>@gmail.com"
password = "<em>yourpassword</em>"
userAgent = "GOOGLE"
applicationToken = "USD"
developerToken = "<em>youremail</em>@gmail.com++USD"
 
; Uncomment to make requests against a client account.
clientId = "client_1+<em>youremail</em>@gmail.com"

In the above code replace ‘youremail‘ with your Google username and ‘yourpassword‘ with your Google password. Before hand you must have signed up to Google Adwords using that username.

In the settings.php file:

you can find the following lines around the 12th line of the file.

;DEFAULT_SERVER = "https://adwords.google.com"
DEFAULT_SERVER = "https://adwords-sandbox.google.com"

Remove the ‘;'(semi colon), before the line that assigns the adwords-sandbox to default_server for working with sandbox environment. After completely developing your App, you can move code live by applying for developer token from Google. Once the developer token is obtained for your MCC account, you can fill your actual developer token in the corresponding variable in ‘Auth.ini’ file and in the ‘clienId’ variable you can fill in the other adwords account you have to manage the ads.

In the settings.php file swap the ‘;’ between the ‘DEFAULT_SERVER’ lines. Thats it now you can explore the API functionalities by running the example files. And Adwords Sandbox doesnt have a GUI so you need to code thoroughly to see the output. Once moving to the live environment all the ads created using API functions will be reflected in the corresponding Campaign.

Google Adwords API – An Introduction

Google Adwords is a platform to spreading business through Internet provided by Google. You can create your own ads for your products. Whenever you create an ad, it gets activates only after passing the policy violation check. After this validation, your ad will start displayed in Google search page and Google Adsense enabled sites.
Account creation in Adwords is free but to create and maintain ads. You need to integrate your bank account or credit cards to your account. Since for every click on your ads, Google will be billing you. You can create any number of ads manually but if you need to have mass creation and maintenance of ads, there comes the API into use. Google provides API's(Application Programming Interface) for most of it's service and Adwords is one of them.

Google describes their Adwords API as:

"The Google AdWords API lets developers build applications that interact directly with the AdWords platform. With these applications, advertisers and third parties can more efficiently and creatively manage their large or complex AdWords accounts and campaigns."

Google API uses SOAP technology for transferring data and information between the server and client machines for RPC(Remote Procedure Calls). You can get client libraries in different languages such as Java, PHP, Ruby, Python, dotnet, Perl. In the following section I am using the PHP client library for the service calls or even if you are interested in using client libraries you can use raw SOAP calls to the specified url with the required parameters in any programming language. You can download the PHP client library from Google code.  

After unzipping the downloaded the package, you can find lots of working examples inside the 'examples' folder and all the source files will be placed under the 'src' folder. In the next post I will explain the configuring and working of the API in test environment. Remember this package will work only on the servers which is configured with SOAP functionality.

Facebook, Twitter and more sharing icons in Drupal

We use social networks to make our website popular and to make it reach more people. Since social networks such as Facebook, Twitter, Delicious, StumbleUpon, LinkedIn has concepts like sharing, liking, following, subscribing, friends status and much more which makes some think on the network to reach more people very easily and quickly. To make your reader share your page comfortably, you can provide them just a 'one-click' share button for each of their favorite social network. Place the below code in your node.tpl.php file where ever you desire.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<div id="sharing">
 
	<div id="facebook_share" class="share_block">
 
	<div id="fb-root"></div>
 
	<fb:like send="false" layout="button_count" data-href="<?php print $base_url.$node_url; ?>" width="100" show_faces="false" font="verdana"></fb:like>
 
	</div>
 
	<div id="twitter_share" class="share_block">
 
	<a href="https://twitter.com/share" class="twitter-share-button" data-url="<?php print $base_url.$node_url; ?>" data-count="horizontal" data-via="goysar">Tweet</a>
 
	</div>
 
	<div id="buzz_share" class="share_block">
 
	<a title="Post to Google Buzz" class="google-buzz-button" href="http://www.google.com/buzz/post" data-button-style="small-count" data-url="<?php print $base_url.$node_url; ?>" data-locale="en_IN"></a>
 
	<script type="text/javascript" src="http://www.google.com/buzz/api/button.js"></script>
 
	</div>
 
	<div id="digg_share" class="share_block">
 
	<!-- Compact Button -->
 
	<a class="DiggThisButton DiggCompact" href="<?php print $base_url.$node_url; ?>"></a>
 
	</div>
 
	<div id="google_share" class="share_block">
 
	<!-- Place this tag where you want the +1 button to render -->
 
	<g:plusone size="medium" href="<?php print $base_url.$node_url; ?>"></g:plusone>
 
	</div>
 
	</div>

This code will place a button with the number of share counts for each of these buttons. To make each of these buttons work we need to include the corresponding JavaScript. We can add the code to include the JavaScript along with this snippet itself, but this code will be duplicated for each node in a page and JavaScript needs to included only once. So we can include the JavaScript in the page.tpl.php file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
	<!-- Twitter button render call -->
 
	<script type="text/javascript" src="//platform.twitter.com/widgets.js"></script>
 
	<!-- Facebook button render call -->
 
	<script>(function(d, s, id) {
 
	var js, fjs = d.getElementsByTagName(s)[0];
 
	if (d.getElementById(id)) {return;}
 
	js = d.createElement(s); js.id = id;
 
	js.src = "//connect.facebook.net/en_US/all.js#appId=248147818554008&xfbml=1";
 
	fjs.parentNode.insertBefore(js, fjs);
 
	}(document, 'script', 'facebook-jssdk'));</script>
 
	<!-- Google+ button render call -->
 
	<script type="text/javascript">
 
	  (function() {
 
	    var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
 
	    po.src = 'https://apis.google.com/js/plusone.js';
 
	    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
 
	  })();
 
	</script>
 
	<!-- Digg button render call -->
 
	<script type="text/javascript">
 
	(function() {
 
	var s = document.createElement('SCRIPT'), s1 = document.getElementsByTagName('SCRIPT')[0];
 
	s.type = 'text/javascript';
 
	s.async = true;
 
	s.src = 'http://widgets.digg.com/buttons.js';
 
	s1.parentNode.insertBefore(s, s1);
 
	})();
 
	</script>

You can customize and add new buttons from the below links.

Twitter

Facebook

Buzz

Digg

Google+

LinkedIn

You can also share, more sharing links in the comment section.

Localize Your Site Contents with Google Translate API

 

UPDATE:Google translate API v1 is not available any more and the update version of Google translate is a paid service. To use Google translate gadget for free, please visit: Localize Your Site Contents with Google Translate Gadget

 

Information shared on a website is public and anyone on the web can get the information. But it is not certain that everyone reaching your site is familiar with the language used in your website. You need not lose your visitors merely because of language conflict. Google comes with a very handle and simple Translator API tool, to translate your site’s contents using AJAX technology(without reloading the page) on changing into any language.

On adding the Google translator API, a dropdown selectbox will be added to your site with list of languages supported by Google Translator. First place the following ‘div’ in your page whereever you like to display the drop down.

 

Then we need to include jQuery plugin and jquery.translate plugin.

 

Following script adds the language dropdown and calls the translate function with the selected language parameter from the dropdown on change.

jQuery(function($){ //when DOM is ready
	jQuery.translate(function(){  //when the Google Language API is loaded
		jQuery.translate.ui('select', 'option')
			.appendTo('#translator') //insert the element to the page
			.change(function(){ //when selecting another language
				translateTo( $(this).val() );
				return false; //prevent default browser action
			 })
		 });
});

After completely loading the page, the API loads the translate script and on success it intializes the UI functions to create the language dropdown with the language list. Along with that a onChange event handler is also defined. Through with the translate function is called with the selected language value.

    function translateTo( destLang ){ //this can be declared in the global scope too if you need it somewhere else
	jQuery('body').translate( 'english', destLang, {   //translate from english to the selected language
		not: '.jq-translate-ui',  //by default the generated element has this className
		fromOriginal:true   //always translate from english (even after the page has been translated)
	 });
    }

You can set the the default language in which the current site has the contents. You can also specify the HTML elements, whose contents need not be translated with their css selector by passing those values in ‘not’ parameter. ‘formOriginal’ parameter makes sure that the contents are translated from the default language always, even after translating the contents into other language, if it was set TRUE.

By default Google will add all the language names in the dropdown in the alphabetic order, so the first language ‘Arabic’ will be displayed in the dropdown box even when the contents arre in the default language. So you can dynamically add text like ‘Please Select’ or ‘Select Language’ as the first element of the drop down to avoid confusions.

    jQuery('
Select Language
 
').prependTo("#translator select");

This works fine when you are using a single page website, if your website has more pages, then you need to translate the contents manually by changing the selected language in the dropdown. We can use cookies to store and reterive the language selected in the previous page. For that you need to download the jQuery Cookie plugin and include the script. To set the cookie for the selected language value add the following code inside the onChange event

    $.cookie("galang", $(this).val(), { expires : 10 });

Then to set the selected language in the dropdown from cookie value add the following line inside the jQuery.translate() function

    jQuery("#translator select").val($.cookie("galang"));
    translateTo($.cookie("galang"));

Thats it. Now your fullsite is localized to most of the familiar languages. Here is the full code.

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script><script type="text/javascript" src="http://jquery-translate.googlecode.com/files/jquery.translate-1.3.7.min.js"></script><script type="text/javascript" src="jquery.cookie.js"></script><script type="text/javascript">// <![CDATA[
	jQuery(function($){ //when DOM is ready
 
	jQuery.translate(function(){ //when the Google Language API is loaded
 
	function translateTo( destLang ){ //this can be declared in the global scope too if you need it somewhere else
 
	jQuery('body').translate( 'english', destLang, { //translate from english to the selected language
 
	not: '.jq-translate-ui',//by default the generated element has this className
 
	fromOriginal:true //always translate from english (even after the page has been translated)
 
	});
 
	}
 
	jQuery.translate.ui('select', 'option')
 
	 .appendTo('#translator') //insert the element to the page
 
	 .change(function(){   //when selecting another language
 
	     translateTo( $(this).val() );
 
	     jQuery.cookie("galang", $(this).val(), { expires : 10 });
 
	     return false; //prevent default browser action
 
	 });
 
	 jQuery('
<option value="">Select Language</option>
 
').prependTo("#translator select");
 
	jQuery("#translator select").val($.cookie("galang"));
 
	translateTo($.cookie("galang"));
 
	});
 
	});
// ]]></script>

Don’t forget to comment on the post. :)

How to restore Windows when GRUB fails

Yesterday, I removed my Debian installation and tried installing Ubuntu 11.04. I use dual boot with windows 7 on the other side, I had to be very careful since i have all my works on windows drives. In the installation process, everything went right until copying the files to hard disk from the installation CD. While copying the files, I/O read error occurred and the installation failed. Already I have deleted the Debian installation which had the boot loader, now I ended up with the GRUB rescue prompt and i lost my windows7. Below i have listed the steps that i followed to get back my windows, people facing kind kind of issues can make use of it.

Procedure 1:

To recover the windows 7 installation, we need windows 7 installation disk. Now try booting the system from installation DVD and choose 'System Repair'. This will lead you to a command prompt where you can type below commands.


    fixmbr

Your damaged GRUB will now be replaced with a new master boot record and the computer should now be able to boot properly. Note that you may also want to run the fixboot command to repair the boot sector with a new one. But there exist a problem in this method, if you don't have any other operating systems installed then this method will be do, but if you have other operating system running, window MBAR can't sense that OS and that can't be recovered. So in those situation we need a Ubuntu Live disk.

Procedure 2:

Now boot the system with the Ubuntu live cd and the package that it is to help us is LILO(LInux LOader). In the live environment, the lilo will not be pre-installed in Ubuntu. So first install the package and restore the loader.


    sudo apt-get install lilo
    sudo lilo -M /dev/sda mbr

And that's it. After running these commands, you can get windows loaded in your system with the other OS installation if available. This is a simple process, but Ubuntu supports only GRUB as boot loader hence this may cause some problem with Ubuntu boot. So this method can be used when you want to get windows restore without having a windows installation drive.

Procedure 3:

If you want to recover windows with GRUB loader, we can use the GRUB rescue method. After booting the system with Ubuntu live disk, in the terminal use.


    sudo grub

Normally grub will not be installed so use


    sudo apt-get install grub

This command will install the grub and on executing the grub command will lead to grub prompt there you can type the following lines to get the boot loader.


    > root (hd0,0)
    > setup (hd0)

Then exit the prompt and restart the system, now GRUB will be reinstalled with all the active operating systems without any issue.

Masking File Extenstions in Netbeans

I recommend Netbeans as the best IDE for working on any kind of projects. Especially, for PHP developers netbeans bags lots of features. Here I am going to share one such feature.

In most of the projects, we are not going to use ‘php’ as the file extension for a php file. Lets say, when working on a custom MVC architecture, we will have controller files as student.controller, model files as student.model and some times view file as studentAttend.tpl.

Even in Drupal CMS, while working on modules we have modulename.module and modulename.test as the file extensions. But in all these case anyway we are going to use php script inside the file. But if you open these files in the IDE, php tags and commands will not be recognized since the file extension is different. For these conditions Netbeans comes with a solution with file extension masking feature. Using that we can instruct netbeans to consider any file extension as any file type. Now open your netbeans IDE and goto


	tools -> options -> Miscellaneous -> files

There under ‘File Associations’, you can select the file extension you want to mask from the ‘File Extension’ dropdown. If you could not find your extension in the menu, you can create a new one by clicking on the ‘New’ button. And enter your file extension in the small pop up box.

After selecting file extension, choose the file type that you want to associate from the MIME drop down. All the file extension supported by your netbeans installation will be listed in that drop down. Once the ‘Ok’ button is pushed your new file extension will be considered as the file type that you have chosen. Enjoy coding.

Don’t forget to comment on the post.