WooCommerce Add to Cart Redirect to Checkout

If you need to redirect the user directly to the checkout page instead of the current product page or cart page, put this snippet to your themes functions.php.

 * Redirect user to checkout page directly after adding to cart
 * @return string 
function wc_redirect_to_checkout() {
    $checkout_url = WC()->cart->get_checkout_url();

    return $checkout_url;

add_filter( 'woocommerce_add_to_cart_redirect', 'wc_redirect_to_checkout' );

Eloquent ORM in WordPress

If you are a plugin writer like me and you write random WordPress plugins for various purposes, chances are you have to write many SQL queries for many cases. But if you look into modern PHP frameworks like Laravel, Symfony, Zend and every other frameworks, you will see they don’t write much raw SQL queries by hand. Instead, everyone is using ORM. In few cases you might have to write raw SQL queries, with ORM you can do that as well.

If you look at the WordPress world, we are still stuck with the WPDB global object. It’s an Active Record class, but can’t be called an ORM. The main reason I like ORM is it reduces so much codes and facilitates many magical features without writing raw queries, instead of manipulate them using an Object Oriented like language. Also you can use many adapters for many databases (though it’s not possible right now in WordPress).

If you are writing a large plugin which involves many custom database tables, chances are you will get lost among those tables and queries very shortly. It happened to me as well and I was looking for using ORM’s in WordPress. But there aren’t so many good tools for that. So I looked into Laravel as it’s huge popular now a days. Laravel uses a package named Eloquent as its ORM provider. As it’s a composer package, you can easily use Eloquent into any PHP application.

There are ways to use Eloquent independently, but it requires you to create a separate connection to database. But as WordPress is already connected to the database, why create a new connection? And if we use Eloquent like that, we will miss database reporting from various debugging plugins like debug-bar, query-monitor, etc.

That’s why I wrote a composer package to use Eloquent ORM inside WordPress using the same database connection using WPDB. The benefits are:

  • Eloquent is mainly used here as the query builder
  • WPDB is used to run queries built by Eloquent
  • It doesn’t create any extra MySQL connection
  • You get to use the WordPress debug tools/plugins

The package is not perfect, but please do checkout the Github repository for detailed information and usage instruction. Pull requests are always welcome.

View on Github

My talk on Open Source Contribution in WordPress

From yesterday (09-02-2015), the biggest tech conference in South Asia is happening at BICC called Digital World Conference, 2015.

All sorts of software exhibition, tech talks, conference are being arranged in this 4 day long exhibition. There is a slot named CMS Developers Conference where we managed developers from WordPress and Joomla to give their talks. It was a blast with about 1300 attendees.

I gave a talk on Open Source Contribution in WordPress and here is the slide link.

Made my first WordPress core contribution!!

Shit just got real, all of a sudden today I made my first WordPress core contribution at this ticket and in this commit :D.

I was checking trac today and stumbled upon this ticket. It was really easy to find and replace some texts. So quickly I submitted a patch and it was accepted within few hours. I just can’t believe that, yes I am excited 😀


Bengali translation, a first time collaboration for WordPress!

Hopefully WordPress 4.0 is going to be released at 27th August, 2014. As this version is going to have language selection from the installation phase, translation is important.

As the validator of WordPress Bengali project, I proposed a translation collaboration effort to give a boost in our Bengali translation. From the banner of our local group WordPressians, today we arranged a “contributor day” to contribute in Bengali translation to WordPress and it was really great. About 30 WordPress lovers joined us in weDevs office to help the Bengali language grow. We already have completed 100% translation in WordPress part, but the Administration area was barely touched (23%). But after the announcement, now we have only about 150 strings left to translate, which was about 1800 before. So that was really an amazing effort.

We have never arranged something like this collaboratively and this worked out really good. It’s always better to do something together rather than doing it alone. More than 50% of them who never translated before. Hats off to everyone who took part at this effort 😀

Some photos…

I will be attending WordCamp San Francisco 2014

I am working with WordPress for about 6 years. Web development is my passion and I fell in love with WordPress very shortly as it was really easy to pick up and get going.

Since then, I wrote many plugins and created a lot of themes and got involved with WordPress. Also I maintain the official Bengali translation project. Though I couldn’t get my head around with WordPress trac and still I couldn’t contribute to WordPress core, I intend to do so in near future. I am watching closely WordPress development and know the community and developers around it through blogs, twitter, github and so on. But as I am living in a third world country, face to face interaction was not an option.

I see people gather in WordCamps and meet other WordPress folks in person, networking is a breeze in WordCamps. I always wanted to attend a WordCamp and this year I was planning to attend one anyhow. I chose WordCamp Europe firstly because of the cultural gatherings it brings. Countries all over the Europe and America gathers on that event, it was the main attraction to me. But sadly, it’s happening on Sofia, Bulgaria and there is no embassy of Bulgaria in Bangladesh. So I had to skip it.

Next target is WordCamp San Francisco. It should have been the first choice, because it’s The Grand WordCamp! But as it’s harder to get an US visa, I was skeptic on this. But as WCEU was out of question, WCSF was the choice. Interestingly, WCSF offered a travel assistance program this time to bring more diversity. I applied for it and luckily, I got selected. Hell yeah! 😀

I got the official invitation letter from Jen Mylo with details. I submitted my visa application on 4th August (this month) and got the visa interview date at 10th August (today), though I didn’t expect the interview date too soon. So today I attended for the visa interview and I got the visa 😀 . Well, to be specific not today, will get the passport back at 13th August.

So, if everything goes right, I will be attending WordCamp San Francisco this year. I hope it’ll be a blast and planning to arrange the first ever WordCamp in Dhaka next year. Fingers crossed 😀

Don’t Lose Your WordPress Translation Files

Happened to many users, may be happened to you as well. You translated a language file in your theme or plugin, but you lost that file when an update came. Your hours of work, lost! That’s why WordPress has a hidden feature just for that.

WordPress loads the language files based on the plugin language scheme. Some prefers to put their language files in the languages folder, or some in lang folder. It’s the plugin authors personal preference. But either way, if WordPress doesn’t find the current language in that plugins/themes folder, it searches on the /wp-content/languages/themes and /wp-content/languages/plugins folder. Note that, it’s the languages folder in the wp-content folder.

If you have wpuf textdomain in a plugin, it’ll search for wp-content/languages/plugins/wpuf-LOCALE_CODE.mo file. Similarly for themes, wp-content/languages/themes/twentyfourteen-LOCALE_CODE.mo.

So if you are translating a theme or plugin and you don’t want to lose the changes when an update comes, place them on the global languages folder. The better approach would be to contact the plugin or theme author and place them in the plugin/theme officially.


My talk on Composer – The missing package manager for PHP

Here in Dhaka, a big technology fair is going on from 4th – 7th June, 2014 at Bangabandhu International Conference Center. Today (6th June, 2014), phpXperts arranged a DevCon with 11 interesting topic and thousands of viewers participated over there. It was such a blast!

Among them I also gave a talk on Composer and here’s my presentation.

Some Pictures