How to send email in WordPress using Gmail’s SMTP server

Well this question was asked to me for more than a couple of times now. So I thought of documenting it here, instead of answering them individually. 🙂

Long time readers of my blog would know that I use Google App for email. Ever since I moved to Linode for hosting, I stopped running a mail daemon in my server, and instead choose to use Gmail’s SMTP server for sending email.

There were two reasons for it. First most email providers ignore or mark as spam, emails sent from non-SMTP servers. Second, removing mail daemon from server will save some resources and will increase the over all performance of my server.

Using SMTP in WordPress

For WordPress there is an excellent Plugin called WP Mail SMTP, which can be used to send email through Google’s SMTP servers.

Download and install the Plugin and in the settings page you have to set the following options.

  • SMTP Host: smtp.gmail.com
  • SMTP Port: 465
  • Encryption: Use SSL encryption
  • Authentication: Yes
  • Username: Your full Gmail address or Google App email
  • Password: your account password

You can also checkout the following screenshot for reference.

using gmail smtp in WordPress

I am also working on a Plugin to log all emails sent by WordPress. Expect it to be released soon 🙂 (Update: The Plugin to log all emails is ready. It is called Email Log and you can download it from the Plugins page.)

Update (Oct 2012)

I just realized that these days Google has added additional security for logging in using other clients. If your email is not sent properly after giving correct password, then refer to instructions at Gmail help to fix it.

Also if you have enabled two factor authentication, then refer to this Gmail support page.

Posted in WordPress | Tagged , , , , | 66 Comments

Post to FriendFeed 1.0

I have updated my Post to FriendFeed WordPress Plugin to v1.0

New Features

The following are the new features that I have added to the Plugin

Pluggable Filters

I have added the following filters to the Plugin which can be hooked by your own code.

  • post2ff_blog_url – The link which will be posted to Friendfeed
  • post2ff_blog_excerpt – The excerpt that will be posted as the first comment to Friendfeed
  • post2ff_blog_images – The images that will be posted to Friendfeed

The following code example explains how you can use bit.ly (or any url shortening service) to shorten the link that will be posted to Friendfeed.

add_filter(‘post2ff_blog_url’,’your_plugin_short_url’);

function your_plugin_short_url($url) {
	$shorten_url = get_short_url($url);
	return $shorten_url;
}

You can place the above code either in your own Plugin or in the functions.php file of your theme. If you want to shorten the url using bit.ly then you can use the code from my other post.

You can use the similar technique to change the excerpt or images, which will be posted to Friendfeed.

Translation

I have added the ability to translate the Plugin. The pot file is available with the Plugin. If you are willing to do translation for the Plugin, use the pot file to create the .po files for your language and let me know. I will add it to the Plugin after giving credit to you.

Update

You can download the latest version of the Plugin form its home page. It is an optional update and doesn’t change anything in the front-end.

Feedback

As usual, if you have any feedback, queries or questions, feel free and leave a comment.

Posted in Plugin Releases | Tagged , , , | 3 Comments

Using BSNL wa3002g4 router as a WIFI switch for Tata wimax connection

Note: If you are not in India or if you are not currently using either BSNL or Tata broadband connection, then you can save a couple of minutes by ignoring this post. 😉

Before I moved to Bangalore, I was having a BSNL broadband connection at Chennai. I bought the wa3002g4 WIFI router from BSNL. Now after moving to Bangalore, I took Tata wimax broadband connection.

Tata wimax doesn’t come with a wifi router and my old BSNL router was lying around. So I thought of using my BSNL router as a switch to convert my wired connection to a wireless connection. After some struggle I succeeded and thought of documenting it here so that it will be helpful for someone who is looking to do the same thing and I can also refer to it in future, if I need to do it again.

Multi-functional device

First thing to know is that, the wifi router (wa3002g4) provided by BSNL is a multi-functional device. It can act as an ADSL modem, a wifi router and also as a switch.

Connecting the wires

First, you need to remove the ADSL cable from the BSNL router and then connect the output cable from Tata wimax modem into any of the four slots of BSNL router. If you need to share the connection with a desktop without wifi card, then you can connect the cable from the desktop network card to one of the other slots in BSNL router. Like wise you connect up to three other computers.

Configuring the router

Now login to your router admin console and disable DHCP. Enter a SSID for your connection and password protect it using WEP or WPA. (WPA is recommended)

Now switch on everything and enable wifi in your laptop. Connect to the router using the password you created. After the connection is established log on to the Tata wimax admin console and enter your username and password. Now you should be able to browse the internet using wifi.

I guess the router can also be used as a switch with other broadband providers, but I am not sure. If you have tried to setup connection with other providers, then do let me know about it in the comments. 🙂

Update: If you are not able to log into the Tata Indicom web dialer page simultaneously in both the computers, then call up Tata Indicom customer care and ask them to enable "NAT" for your connection. Once enabled, you can use connect using multiple computers, without individually logging in each of them separately.

Update 2: I have since moved on from Tata Indicom connection. I currently use this router to share WiFi signal across floors in my home.

Posted in Gadgets | Tagged , , , | 83 Comments

Find duplicates values in a set of textboxes using jQuery

Well sometime back, I faced this unique problem. It took me sometime to solve it, so I thought of documenting it as my learning so that it is useful for others and might help me one when I need to do it again.

Problem

Okay, the following is the requirement. You have a web page with a set of text boxes. The number of text-boxes might vary based on some backend logic. You need to make sure the user doesn’t enter duplicate values in these set of textboxes. He can leave them blank, but cannot enter duplicate values.

Solution

The following is my solution.

function findDuplicates() {
    var isDuplicate = false;
    jQuery("input[name^='access_keys']").each(function (i,el1) {
        var current_val = jQuery(el1).val();
        if (current_val != "") {
            jQuery("input[name^='access_keys']").each(function (i,el2) {
                if (jQuery(el2).val() == current_val && jQuery(el1).attr("name") != jQuery(el2).attr("name")) {
                    isDuplicate = true;
                    jQuery(el2).css("background-color", "yellow");
                    jQuery(el1).css("background-color", "yellow");
                    return;
                }
            });
        }
    });

    if (isDuplicate) {
        alert ("Duplicate values found.");
        return false;
    } else {
        return true;
    }
}

I basically loop through all the text boxes and compare the values with all the other textboxes, in nested loops. Basically it is like bubble sort. I know this is bad, but was not able to think of any efficient way of doing it. Do you guys can think of a better way?

Posted in JavaScript/jQuery | Tagged , , | 4 Comments

Disable/Enable retweet button for each post

Okay, yet another update to my Easy Retweet Plugin. 🙂

Configure retweet button for each post

Now you can enable or disable the retweet button for each post or page. When you update to the new version, you will see a new box on the right hand side of the write post/page screen where you can enable or disable the button.

This setting will over ride all the other options which are set in the admin console page.

Screenshot

Easy Retweet WordPress Plugin Screenshot

Removed the hard coded wp-content path

I have also removed the hard coded reference to the wp-content path. This means that the Plugin will work for people who have moved their wp-content to a different folder.

This change involved a hack to make the JavaScript work properly. I will write an article explaining this hack in detail, meanwhile if you can’t wait then check out the source code. It is well documented. 🙂

Download

You can download the latest version of the Plugin from the Plugin’s homepage.

Feedback

Please keep the feedback coming and if you want me to add any new features or find a bug, please leave a comment.

Vote for the Plugin

If you have used this Plugin and like it, please vote it and help me win the WordPress Plugin competition.

Posted in Plugin Releases | Tagged , , | 5 Comments

Using WordPress built-in tag auto complete script in your Plugins

When I released my Posts By Tag WordPress Plugin, I promised to write a post explaining how to use the built-in auto complete tags script in WordPress, and so here is how you do it. 😉

jQuery Plugin

WordPress has a jQuery Plugin called suggest, which will do the heavy lifting for us. To use this Plugin, you have to first enqueue the script using the wp_enqueue_script() function

// Register hooks
add_action('admin_print_scripts', 'add_script');

/**
 * Add script to admin page
 */
function add_script() {
    // Build in tag auto complete script
    wp_enqueue_script( 'suggest' );
}

Once the script is included, you have to just call a method called suggest on your jQuery collection and everything else will be taken care for you.

<?php
/**
 * add script to admin page
 */
function add_script_config() {
?>

    <script type="text/javascript" >
    // Function to add auto suggest
    function setSuggest(id) {
        jQuery('#' + id).suggest("<?php echo get_bloginfo('wpurl'); ?>/wp-admin/admin-ajax.php?action=ajax-tag-search&tax=post_tag");
    }
    </script>
<?php
}
?>

If you want to support multiple tags, then you can use the following options.

  • multiple – to enable multiple tags to be entered, pass true
  • multipleSep – The separator symbol for multiple tags

so the code will be

<?php
/**
 * add script to admin page
 */
function add_script_config() {
?>

    <script type="text/javascript" >
    // Function to add auto suggest
    function setSuggest(id) {
        jQuery('#' + id).suggest("<?php echo get_bloginfo('wpurl'); ?>/wp-admin/admin-ajax.php?action=ajax-tag-search&tax=post_tag", {multiple:true, multipleSep: ","});
    }
    </script>
<?php
}
?>

You can also use this script to auto complete other custom taxonomies, which were introduced in WordPress 2.8. For that you have to change the tax parameter in the url. The following are the default taxonomies that are available.

  • post_tag
  • link_category
  • category

In addition to them you can also use your custom taxonomies.

So the complete code is

<?php
// Register hooks
add_action('admin_print_scripts', 'add_script');
add_action('admin_head', 'add_script_config');

/**
 * Add script to admin page
 */
function add_script() {
    // Build in tag auto complete script
    wp_enqueue_script( 'suggest' );
}

/**
 * add script to admin page
 */
function add_script_config() {
?>

    <script type="text/javascript" >
    // Function to add auto suggest
    function setSuggest(id) {
        jQuery('#' + id).suggest("<?php echo get_bloginfo('wpurl'); ?>/wp-admin/admin-ajax.php?action=ajax-tag-search&tax=post_tag", {multiple:true, multipleSep: ","});
    }
    </script>
<?php
}
?>


The only cavet to this method is that right now the admin-ajax.php file needs you to be logged in and therefore can only be used in admin pages. But in WordPress 2.9 even anonymous users can load admin-ajax.php file. If you need use auto tag completing in blog pages, then you may have to wait till 2.9 is released 🙂

Posted in WordPress | Tagged , , , | 26 Comments

Associating urls created with bit.ly API to your account

Recently while working on adding the feature to enter your own bit.ly API key to my Easy Retweet WordPress Plugin, I found out that by default all short urls created using bit.ly API (both REST and JavaScript API’s) are not associated with your account.

Upon further researching I found that there is an undocumented way of associating the urls created with your account. The undocumented way is to add an additional parameter called history with value 1 to the API URL.

Using the history parameter in bit.ly’s REST API

So for REST API, you have to use the following url.

http://api.bit.ly/shorten?version=2.0.1&longUrl=http://sudarmuthu.com&login=bitlyapidemo&apiKey=R_0da49e0a9118ff35f52f629d2d71bf07&format=json&history=1

If you are using PHP, then code would be

function get_bitly_shorturl($longurl) {
	$url = "http://api.bit.ly/shorten?version=2.0.1&login=bitlyapidemo&apiKey=R_0da49e0a9118ff35f52f629d2d71bf07&format=json&history=1" . "&longurl=$longurl";

	//using curl
	$curlObject = curl_init();
	curl_setopt($curlObject,CURLOPT_URL,$url);
	curl_setopt($curlObject,CURLOPT_RETURNTRANSFER,true);
	curl_setopt($curlObject,CURLOPT_HEADER,false);

	$result_json = curl_exec($curlObject);
	curl_close($curlObject);

	//decode JSON. Assumes that it is PHP5
	$result = json_decode($result_json);

	return $result['results'][shortUrl];
}

If you are going to use it in WordPress, then you can use the inbuilt WP_Http class instead of curl as suggested by Ozh. The following code shows you how it can be done in WordPress

function get_bitly_shorturl($longurl) {
	$url = "http://api.bit.ly/shorten?version=2.0.1&login=bitlyapidemo&apiKey=R_0da49e0a9118ff35f52f629d2d71bf07&format=json&history=1" . "&longurl=$longurl";

	//using WP_Http present in WordPress
	$request = new WP_Http;
	$result_json = $request->request($url);

	$result = json_decode($result_json);

	return $result['results'][shortUrl];
}

Using the history parameter in bit.ly’s JavaScript API

If you are using bit.ly’s JavaScript API, then it is not as straight forward as the REST API. Instead of using the provided shorten method, you have to use the low level call method.

The following code shows you how you can do it in JavaScript API.

BitlyClient.call('shorten', {'longUrl':'http://sudarmuthu.com', 'history':'1'}, 'BitlyCB.shortenResponse');

I am not sure why bit.ly is not associating the created shorturls automatically with your account, when you provide the API Key, it is the expected default behavior. Or at least they could have documented about this history variable in their API. I guess only someone from bit.ly can answer this. 🙂

Posted in API's/Mashup, JavaScript/jQuery, WordPress | Tagged , , | 8 Comments

Proper way to display logout link in WordPress

Recently, I was debugging an issue with logout for a friend who was running a WordPress blog. His theme had a logout button in the sidebar, which stopped working after he upgraded to the latest version of WordPress from a pretty old version.

After some debugging, I found that the nonce value was missing from the logout link. In older versions of WordPress, the nonce value was needed, but in newer versions, we need to pass the nonce value to perform the logout.

wp_logout_url

We need not append the nonce value manually, instead we can use build-in function wp_logout_url(). This function will automatically append the nonce value to the logout link.

If you want the user to be redirected to the current page he is viewing, after logging out, you can use the following code snippet.

<a href="<?php echo wp_logout_url();?>&redirect_to=<?php echo $_SERVER['REQUEST_URI']; ?>"><?php _e('Logout'); ?></a>

Posted in WordPress | Tagged , | 1 Comment

Posts By Tag 0.3

Just a quick note, to let you all know that I have updated my Posts By Tag WordPress Plugin to version 0.3

Features

The following are the changes in the new version

Improvements to caching

Now the Plugin caches the entire HTML generated by the widget, not just the query which is used to retrieve posts. This should improve the performance of the Plugin and will reduce some mill-seconds of your page load time, if you have enabled caching.

Turkish Translation

The credit for this goes to Yakup Gövler. I have checked in the Turkish .po and .mo files, in this release.

If you are willing to do translation for the Plugin, use the pot file to create the .po files for your language and let me know. I will add it to the Plugin after giving credit to you.

Update

You can download the latest version of the Plugin form its home page. It is an optional update and doesn’t change anything in the front-end.

Feedback

As usual, if you have any feedback, queries or questions, feel free and leave a comment.

Posted in Plugin Releases | Tagged , , | 4 Comments

It’s been 30 days since I abandoned TV

.. and I am really happy about it now. 🙂

Like my role models, I thought of abandoning TV as a 30 day experiment and it worked out wonders. 🙂

It all started after I moved to Bangalore. I rented a new house and I needed to buy a set-top box to watch TV. I was slightly busy getting settled in the first week and so was not able to do it immediately. After about a week of not watching TV, I thought of continuing it for a month, to see how things turn out.

Now, after a month, I am very happy that I took the decision to abandon it. I was able to save at least 3-5 hours a day and I tried to put in those hours to other things that I always wanted to do, but didn’t had time to do. Following are some of the things that I did in the hours that I saved.

Concentrated more on my blog

I concentrated more on my blog with the extra time I got and it has yielded very good results. Here are some stats.

  • In July alone, I have written 16 posts, which is the highest number of posts in a given month in the entire existence of my blog.
  • On July 24th, I received the highest number of page views on a single day in the entire existence of my blog.
  • In July, I have received the highest number of page views on a single month.
  • In July, the traffic to my blog has increased by more than 40%.

WordPress Plugin

Next within these 30 days, I have created and released more than 5 Plugins for the WordPress Plugin competition and have updated around 3 of my old Plugins. Ohh, by the way, do vote for my Plugins 😉

Gym

The next best thing, I did during this period is that, I have started going to the gym regularly, which I always wanted to do (but didn’t) for the last 10 years. I could feel that my stamina has increased tremendously after just one week of regular workouts.

Books

I was also able to finish reading around 5 books, which were lying in my bookshelf without reading for quite some time.

Other projects

I have also invested the extra time in a couple of other projects (which I will talk about soon). I am also working on enhancing the theme that I am using for my blog and you will see it in action with in a week.

Future plan

I am planning to continue abandon TV and planning to go to theatres to watch movies. May be I will relax this rule a bit, when cricket matches take place.

Overall, I was able to achieve, what I wanted to achieve for years, by just switching off the TV for 30 days. More than achieving things, I am very happy and much relaxed now. No wonder why they say that people who don’t watch TV are very happy. 😉

So what are you going to do for your 30 days experiment? 🙂

Posted in Random/Personal | Tagged , , | 14 Comments