WordPress debugging in phpStorm

While working with WordPress after a while, the first problem I encountered is that I can’t debug it easily as I could do it directly from PyCharm (for python/django), RubyMine (for ruby/rails). So, I wanted to figure out how to do it in phpStorm for WordPress. I found it is quite easy to get it done with a few easy to follow steps.

phpStorm 8 (currently available in EAP) has support for WordPress development. Follow the official doc to configure phpStorm for WordPress.

Enable Remote Debugging using xdebug

Check the official document on how to install xdebug extension for PHP and enable it. Once the extension is installed, you need to enable it and also enable remote debugging.

[xdebug]
zend_extension="/Applications/MAMP/bin/php/php5.5.10/lib/php/extensions/no-debug-non-zts-20121212/xdebug.so"
xdebug.remote_enable=1

The above is my configuration. However, you may need to change the path of xdebug.so based on your extensions path.

Once installed, restart your web server. To make sure xdebug is activated, run

php -v

This should output something like:

$ php -v
PHP 5.5.10 (cli) (built: Apr 10 2014 17:49:22)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies
with Xdebug v2.2.4, Copyright (c) 2002-2014, by Derick Rethans

Notice that Xdebug 2.2.4 is activated. You can also see this using phpinfo().

Configuring phpStorm

As we’ve installed Xdebug, let’s link it with phpStorm. Open phpStorm’s preference and

Screenshot 2014-08-07 21.08.55

Select version from PHP language level and interpreter where you’ve just configured the xdebug. If it’s not listed, click on the Screenshot 2014-08-07 23.01.03 button.

Screenshot 2014-08-07 21.09.54

Give your interpreter a name. For PHP home, click the Screenshot 2014-08-07 23.01.03 button next to the field and locate your php interpreter. If you don’t know where it is go to your shell and run the following command to get the location.


which php

It should detect that Xdebug is installed in this interpreter. Press Ok buttons.

Now, you need to tell phpStorm to listen for the Debug connections. In the toolbar, you will notice the button shown in the following screenshot. Click there and phpStorm will start listening for debugger connections.

Screenshot 2014-08-07 21.11.45

Triggering the debugger

We’re in the last part of our configuration. Everything is done except triggering the xdebug. There are several ways of triggering the debugger. Easiest way is probably using browser extensions. Install extension for your browser as listed below. I will show for Chrome.

Firefox

Chrome

Safari

Opera

After installing the extension, you’ll notice a new button at the right side of your address bar.

Screenshot 2014-08-07 23.07.44

Click on it and choose Debug option.

That’s all*. Now set a breakpoint in your IDE and the execution will be paused there when you refresh your page.

Screenshot 2014-08-07 23.12.23

You can now step into different levels, evaluate expressions on current context, see all scope variables and so on. Your WordPress development will never be painful like earlier. Follow this link to learn more about debug toolbar.

Screenshot 2014-08-07 23.14.34 * First time, you will receive a prompt in your IDE to accept the incoming connection.

 

Handlebars helpers

In an application we are using Handlebars templating. However, we needed many custom helpers to do what we wanted to achieve. Here are some of those helpers. It may help someone who is looking for achieving similar things. Examples are included.

Adding/Subtracting

Character limit with trailing characters (optional).

Get free SMS alert on your server down (or something else)!

Well, first thing first. This blog may be little bit immoral even if it is not illegal by the provider terms of service. So, if you read this or later sentences, the responsibilities are on you.

—————-

I am going to share an idea that I’ve personally used a bit ago (2009) in my previous office to monitor the server and get notified when any server/service is down. I wrote a small PHP application that would do the tasks. Though I planned that time to blog about it, apparently, I forgot about it. The steps of the idea is:

  • A php chunk checks server status
  • If it finds something unexpected (or may be expected; depends on your logic :)) it creates an event in your google calendar which is due just 1-3 minutes from now
  • If your SMS alert is setup in Google Calender, Google will email you about the event.

Here I will not show how to write a PHP script to monitor server. I just told it as I applied this idea for same purpose. I guess you will get lots if you google it. However, the initial version that I developed just checked site’s homepage (using CURL) and if they were returning 200 response within a sensible time limit. If the response was not 200, it would create the event for me.

How to create Event?

It’s very simple. First of all download the following classes from phpclasses.org

Put them in your project path. Create a php file. Lets assume it is createevent.php. Now include the wrapper file in createevent.php.

Continue reading →

Line wrapping is finally available in Netbeans 7.0

Well, if you ask me why I left Netbeans and started using phpStorm; I will say first: Speed, second: Line wrapping along with few other reasons. I’ve used Netbeans for quite sometime but I was very irritated as it did not have good line wrapping support. Some users may wonder why I am saying ‘good’ as it did not have any line wrapping at all.

The fact is that, line wrapping was implemented in earlier versions too but was not enabled by default as it was not stable.  It can be enabled by editing a configuration file but it was causing lots of troubles while coding. However, as the latest version, v7.0, is released on 18th April 2011, line wrapping is now available without any such hacking and so far it is good to me.

How to enable Line Wrapping?

Go to Netbeans  -> Preferences (or press CMD + ,) -> Editor -> Formatting

You will notice a drop down labeled as ‘Line Wrap’. Choose ‘After Words’ or ‘Anywhere’ as per your wish and then hit OK.

That’s it. Simply open a file (opened files need re-opening) and you will not see horizontal scroll-bars :).

Netbeans Linewrapping

Netbeans Linewrapping

Extending or replacing core classes in CodeIgniter 2x

It has been quite some time that CodeIgniter 2x has been released after a quite long waiting. There are several improvements in this latest release and upgrading from the 1x is quite easy. However, they have changed some file/folder organization in this latest release. I found these changes as good as it provides better application wise personalization. CodeIgniter’s previous file/folder structures were not (at least, by default) very good (without some/little modification) for hosting multiple application based on single core installation. However, in this version they organized it better.

On of the major change was made in this version in replacing and/or extending core classes. There was no note in their upgrade guide (but explains in their relevant doc page, which I found later). So I had to go through little trouble with it until Arafat Rahman helped me out!

However, the process is simple. In version 2x, they have introduced a new directory called ‘core’ (along with few others). All core classes that needs to be replaced or extended should be in this directory. I liked it very much, as I hated to put core classes in library directory in older versions.

Replacing a core class
Let’s say we want to replace a core class by our own version. Also let’s assume we want to replace the model class (actual name is CI_Model). Now follow these steps:

  • Create a directory named ‘core’ in your application directory, if it does not exist. If your application directory is default one, it would be ‘application/core’
  • Create a new file named Model.php.
  • Put the following line in the newly created empty file:
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
class CI_Model
{
 function __construct()
  {
  }
}

Now when you create model classes in current application extend the CI_Model as you were doing earlier, this class will be extended rather than the CI_Model class in the system/core/Model.php. However, when you do, think if you have enough reasons to replace the core class as you can do most, if not all, of the tasks by simply extending it.

Extending the core class

Rather than replacing, I find it much better to extend the core class and use that class in my application. The steps of extending the core class are also simple. Continue reading →

Pun Warning – Warning system for PunBB

Quite long ago, I have developed this extension to warn users due to violating rules in Projanmo Forum. Many asked me for releasing it but I could not release it for various reasons mainly as I could not test in Non-Bangla forums.

Today I decided to release it and so I’ve downloaded punbb and installed and then tested it. Seems it is working fine. So, I am now releasing it.

1. Download and install it as you do other extensions. You will also need to install jQuery extension before installing it. Continue reading →

My experience with phpXperts seminar 2010

phpXperts Seminar 2010, is my third program with phpXperts. Other than phpXperts programs, I have also joined in other programs like Facebook Developer Garage, SQABD Lightning Talks etc. However, this one is exceptional to me as I was one of the speakers in this session.

My initial gratitude goes to Hasin Hayder, who tried hard for first speakers like me. He even advised me about my topic “HTML5 – Web is getting sexy”! Continue reading →

Searching and retrieving yahoo answers in PHP

I am working on an WordPress autobloggin plugin where I had to search yahoo answers site for any keyword and post the answers in WordPress. After checking their site, I see it is very easy now as they provide API for the questions and answers. I do not know when they introduced API for Answers but I do remember during my last check (quite long time ago), they did not have any API and scrapping was the only way.

Their API for searching and getting answers is very simple and easiest! However, for the better handling of my project, I have mad a class to find questions by any given keyword and then retrieve the answers of any specific question by question ID.

How to use?
Continue reading →

Case inSensitive in_array()

You might already know that PHP’s in_array() function matches the string needle case sensitive way. However, today I was needed something that matches in case insensitive way. Before doing it myself, I googled it and I found a ready made solution. It is quite simple. Here it is:

function in_arrayi($needle, $haystack)
{
    foreach ($haystack as $value)
    {
        if (strtolower($value) == strtolower($needle))
        return true;
    }
    return false;
}

Please note that this case-insensitive version is, at least, 5 times slower than in_array().

Using prosperent.com API with PHP!


Currently I am working with Prosperent.com’s affiliate network and making an auto blog plugin for wordpress. The plugin is intended to fetch products from prosperent and create wordpress posts. You will earn commission from the sales through your affiliate link! The affiliate links will also be given by prosperent. So, you don’t need to think about it. You just need to requests their API with the API Key and they will mask the product URL with your affiliate ID so the impressions and clicks are accounted under your affiliate account.

Prosperent already provides a simple and beautiful PHP class that makes enjoyable while working with their API. You just need to write a few lines to get products from them.

Continue reading →