Creating a slack bot using PHP to monitor your envato sales

Slack is awesome. Slack is the de-facto of modern team communication and I am pretty sure that this is not the first time you’re hearing something like this about slack. But if you’re really never used slack, oh boy, I feel pity for you :)

Anyway, in this article we’re going to make a slack bot that will monitor the sales in our envato account and notify from time to time as soon as someone purchased an item. Pretty useful, eh?


Considering you have a slack account, log into it and open a new channel, for example “#sales”. Now go to or just click on “Configure Integrations” from your slack app.

Continue reading Creating a slack bot using PHP to monitor your envato sales

Packaging thirdparty plugins with your WordPress theme using TGM Plugin Activation library

When you’re shipping your theme for sell, it’s often required to bundle the required plugins. Some people just keep these plugins like general vendor libraries inside their theme and load them using php’s require_once. However, if you’re following this style, your themes will fail to pass the theme check because these plugin files will be considered as a part of your theme, and it will prompt you that CPT functions and shortcodes should be a part of the plugin. Moreover, if someone deactivate your theme, those shortcodes and CPTs will be unavailable immediately.

This is why, along with many other developers, we use TGM Plugin Activation library. This tiny little lib can force or suggest installing the plugins which are required (or good to have) for making your WordPress theme work flawlessly.

TGM can work with plugins which are hosted in WordPress plugin repository, or in a github public repository. It can also work with plugins which are shipped with the theme as a separate zip file. In this article we’ll see all these three cases of plugin installation and activation process using TGM library.


Download the TGM library from After unzipping, keep the class-tgm-plugin-activation.php file inside vendors/tgm/ inside your theme. Then add the following code in your functions.php Continue reading Packaging thirdparty plugins with your WordPress theme using TGM Plugin Activation library

Generate Dummy Placeholder Images in the WordPress Export File Instantly

It’s a common problem for the theme developers who sell their themes in the marketplaces and ships a data file with it, that contains exported data from the demo WordPress site. The problem is, this content file contains links to the images used in the demo. And those images, which the author uses to decorate his demo sites, may not be freely distributable.

So, theme developers have to create another site with placeholder images and then export data from this site. It’s a double work. It’s a boring work as well.

To save from this pain, I’ve created a site called DimgX which is available at that can deliver placeholder images on the fly. Using this service is pretty straight forward. If your image url is all you have to do is add a at the end of the image domain, like

So if the url of the original image is which displays like this Artwork By Greg Christman

It’s placeholder image’s url will be and it will show like this – Tada! You don’t have to worry about the original image’s dimension or anything. Everything will be handled by DimgX on the fly.

Using this feature, DimgX allows you to upload the exported content file from your WordPress site, which has an xml extension, and then download a modified content file having all the attachment urls replaced using It’s pretty handy

DimgX replaces all the <guid> and <wp:attachment_url> in the export file, which contains links to the image files, and adds at the end of those domain names in those image urls.

While creating a placeholder image, DimgX needs to calculate the size of the original image, to properly draw a placeholder with the same dimension. And in PHP, you know the only way to do it using getImageSize() function. People usually download whole image and then calculate these dimensions but that’s is a pretty expensive process when you have to download lots of bigger images. Fortunately, all these popular image formats contains these dimension in the first few KB in their header. So DimX doesn’t download the whole image but only a chunk of it. And then it uses that chunk of the header data to calculate this size. It makes the whole process fast, and less resource extensive. And while doing this, it also saves a good amount of bandwidth. To feed your appetite, here’s a part of that routine

switch ($extension) {
    case "jpg" :
    case "jpeg" :
        $range = "0-50000";
    case "png":
        $range = "0-10000";
    case "gif":
        $range = "0-10";
        $range = "0-15000";

$ch = curl_init ($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
curl_setopt($ch, CURLOPT_RANGE, $range);

And here is the regular expression pattern that gets this complex URL replacement job beautifully done

$pattern1 = "/<guid([^>]+)>http://([^/]+)/([S]+)(.jpg|png|gif)</guid>/";
$pattern2 = "/<wp:attachment_url>http://([^/]+)/([S]+)(.jpg|png|gif)</wp:attachment_url>/";
$new_data = preg_replace($pattern1,'<guid${1}>http://${2}${3}${4}</guid>',$data);
$new_data = preg_replace($pattern2,'<wp:attachment_url>http://${1}${2}${3}</wp:attachment_url>',$new_data);

Anyway, feel free to use DimgX at and let me know how you feel about this service. Right now, it can convert image urls that is served using http://, because wildcard SSLs are not cheap. But it’s in our roadmap. Stay tuned :)

[update] PHPStorm’s upcoming subscription based licensing model and why you’re fucked up

** I’ve added an update. Click here to read it **

JetBrains has sent me an email today, mentioning about the upcoming change in their licensing model. Right now all users can renew their license at $49 which is a 50% discount rate of the original rate. From this November 2nd, you will be able to renew it for $119. Or you can buy a new license for $199/yr or $19.90/mo. Now, if you want to do some simple math, the upcoming yearly cost will be a whooping 100% increased price. If you go for monthly subscription, it will be like 141% extra of the current price.

Moreover there will be pain if you don’t want to continue. If you start with, for example V8, and in the middle of a yearly subscription V9 is released, but you don’t want to continue after the end of your subscription – you will be given a perpetual license of V8. You read it right, you will have to continue with V8 – not V9. Even though V9 was released during your subscription, you will not be allowed to keep using it. Huh!

Well, either way you’re pretty much fucked up. If you’re an existing license holder then you may find it slightly profitable to stick for the first two years because they said that there will be a 40% discount and 2nd year will be on them.

I am using PHPStorm since it’s version 3 and I am not going to renew it this time, for sure. I’ve been experimenting with Atom lately and it is pretty nice. Anyway, I am going to miss PHPStorm because I consider their upcoming licensing model is a kinda ripping-off of their loyal customers. Dear Jetbrains, Adios!


Ok, it looks like I was comparing the pricing for company/organization instead of the personal licensing model. Their page content is pretty confusing, and it’s hard to get it right at the first sight. I’ve found the correct page with the pricing for personal/individual license and it’s not bad.

With this pricing, well, I think I don’t have any problem to renew it again. Thanks to @Gary Hockin and @Hadi Hariri from Jetbrains for clearing it up.

Chrome broke WordPress Admin Panel – here’s how to fix that

A recent update of Chrome browser broke the WordPress admin panel. It happened because of a setting related to slimming paint. Of course you can fix it by going to chrome options and then by disabling it. However, your client may not be able to do it because it’s a pretty confusing setting. To stay in the safe side, it’s nice to add this little snippet in your theme and it will make sure that everything works nicely

function admin_menu_chrome_bug_fix() {
    echo '<style>
        #adminmenu { transform: translateZ(0); }
add_action('admin_head', 'admin_menu_chrome_bug_fix');


Custom post pagination in WordPress

If you’re dealing with lots of custom posts in WordPress and if you’re a theme developer, it is probably a well known issue to you. Lots of people are discussing about this pagination thing in different forums. However, it’s really not that tough to paginate custom posts. Let’s find out how we can do it easily.

Query CPTs

This is the first thing you are going to need, is to write a custom query. Because when you have a bunch of posts in your collection, you will start thinking about the pagination. So it’s time to find some posts. The following code will find all the posts of post type book and display their titles

$books = new WP_Query( array(
"posts_per_page" => - 1,
"post_type" => "book"
) );

while ($books->have_posts()){
echo "<br/>";

Now you’re probably going to use the good old next_posts_link() to display the next page. Well, that will not work because at this point next_posts_link() doesn’t know how many pages are there. So to make it work, you’ll have to pass the number of total pages found by your query. How is this total number of pages are calculated by the way?

$total_pages = ceil( $books->found_posts / get_option('posts_per_page') );

Interestingly, you don’t need to calculate the total number of pages like this. A WP_query object already did it for you and made it available via max_num_pages property.

$total_pages = $books->max_num_pages;

Now we need that value to show the link to our next page. Just pass it to next_posts_link() like this

next_posts_link('Prevous Posts',$books->max_num_pages)

Now it wil show a valid link that can take you to the previous posts page. If your url is then, the link of the previous posts will be of this format So you’re going to need this page number in your PHP script to fetch all the posts (or CPTs) from that page. In your theme, you can get this page number using a query var called paged. The following code will correctly fetch the current page number

$current_page = get_query_var('paged') ? get_query_var('paged'):1;

To fetch all the posts from this page, you just need to rewrite your WP_Query like this

$books = new WP_Query( array(
"posts_per_page" => - 1,
"post_type" => "book",
"paged" => $current_page
) );

In case you’d like to see a traditional pagination using page numbers, you will have to use the following code

paginate_links( array(
"current" => $current_page,
"total" => $books->max_num_pages
) );

And you’re done :)

Using OAuth2 to make authenticated calls to WP REST API


WP REST API is a cool solution for the developers who want to interact with their WordPress blogs programmatically. It’s handy and pretty useful. However, the documentation lacks some working examples which makes a lot of people confused. I’ve started to document some working examples at The list is small, but it’s growing.

Anyway, I’ve made a small video tutorial last night to demonstrate how to authenticated yourself using OAuth2 and make some authenticated calls to your WP REST API endpoint. There are hundreds of confused users floating out there looking for a solution, and I hope this video will fix it. Let me know if you find it useful