Adding dynamic classes to your custom WordPress widgets

I recently released an ad management plugin for WordPress called “Easy AdSense Ads & Scripts Manager“.

In this plugin we have a custom widget with option to remove the padding and borders (if any) added by the theme. I did not think much before implementing this feature and was expecting WordPress to have some kind of hook or filter to easily add a class to the widget wrapper and use it to remove the padding and border.

Option that lets you remove padding and borders from the widget

Unfortunately for some reason there is no easy hook or filter in this case. Very strange for WordPress.

But luckily there are two things available

  1. You can add any custom classes to your widget.
  2. There is $args['before_widget']

So here is what I did, use option one to add a custom class, which is actually a place holder which I would replace later. Like this

Now whenever the user checks the remove padding and border option,

I would simply replace this __eaa__ with the class eaa-clean like like below

I could have done this directly skipping the first step of adding a placeholder and directly using a regular expression on $args['before_widget'], but did not for the following reasons.

  1. It did not seem solid, and might fail in some cases.
  2. Since this a plugin that can be used in conjunction with any theme, I don’t have control over what $args['before_widget'] is going to be.

Complete implementation of this can be found on git

2 Common errors while setting up phpMyAdmin on Mac OSX

You might encounter the following two errors while installing phpMyAdmin on OSX

  • Login without password is forbidden by the configuration ( See AllowNoPassword )
  • #2002 can not login tot the MySQL server

Here are the fixes for the above two errors.

1. Open phpMyAdmin configuration located at (phpMyAdmin Folder)/config.inc.php and change

$cfg['Servers'][$i]['AllowNoPassword'] = False; to $cfg['Servers'][$i]['AllowNoPassword'] = true;

2. In the configuration file change  $cfg['Servers'][$i]['host'] = 'localhost'; to $cfg['Servers'][$i]['host'] = '127.0.0.1';

Trying cakePHP, lets see how it goes.

My growing list of direct AD sales was becoming hard to manage, dealing with direct AD sales was taking more time than it should actually take. It’s not streamlined. I lost AD revenue several times because I was too lazy to mail the advertisers about the renewal of their adspace.

I always wanted to automate the AD sales process, but I was busy with something or the other. Now that I got some free time to explore new stuff, I decided to take up this project.

Goals of the project

  • Maintain a list of advertisers and the AD space they bought.
  • Automatically send reminders to advertisers about renewals.
  • If they fail to renew, notify me, so that I can take down the AD.
  • A web interface where I can add details about the newly bought ads to the DB and make changes to previously bought ADS.
  • *Update the DB automatically when a payment is made through PayPal.
  • *Setup a AD server, which will let me control AD’s on all the sites without from a single location.

I’m going to implement this project using cakePHP, I hope cakePHP is the right choice.

 

Subtracting two dimensional arrays in PHP

In the development a WordPress plugin I had to subtract two dimensional arrays. I tried array_diff() function, unfortunately it doesn’t seem to work for two dimensional arrays. Even if it worked, it wasn’t exactly the functionality I wanted.

Be warned, this isn’t exactly a alternate for array_diff(). For difference of two dimensional arrays you may try this (I dint try it)

I had these two arrays, which I had to subtract.

[php]
Array (
[thumbnail] => Array ( [width] => 150 [height] => 150 [crop] => 1 )
=> Array ( [width] => 300 [height] => 300 [crop] => )
[large] => Array ( [width] => 1024 [height] => 1024 [crop] => )
[full-post-thumb] => Array ( [width] => 576 [height] => 150 [crop] => 1 )
[popular-posts-home] => Array ( [width] => 210 [height] => 90 [crop] => 1 )
[popular-posts-widget] => Array ( [width] => 32 [height] => 32 [crop] => 1 )
[mag1] => Array ( [width] => 170 [height] => 0 [crop] => 1 )
[mag2] => Array ( [width] => 284 [height] => 0 [crop] => 1 )
[blog-thumb] => Array ( [width] => 135 [height] => 90 [crop] => 1 )
[slider1] => Array ( [width] => 250 [height] => 130 [crop] => 1 )
[slider2] => Array ( [width] => 576 [height] => 280 [crop] => 1 )
)

Array (
[full-post-thumb] => Array ( [width] => 576 [height] => 150 [crop] => 1 )
[popular-posts-home] => Array ( [width] => 210 [height] => 90 [crop] => 1 )
[popular-posts-widget] => Array ( [width] => 32 [height] => 32 [crop] => 1 )
[mag1] => Array ( [width] => 170 [height] => 0 [crop] => 1 )
[mag2] => Array ( [width] => 284 [height] => 0 [crop] => 1 )
[blog-thumb] => Array ( [width] => 135 [height] => 90 [crop] => 1 )
[slider1] => Array ( [width] => 250 [height] => 130 [crop] => 1 )
[slider2] => Array ( [width] => 576 [height] => 280 [crop] => 1 )
)

The result I wanted was this, but array_diff() returned null.

Array (
[thumbnail] => Array ( [width] => 150 [height] => 150 [crop] => 1 )
=> Array ( [width] => 300 [height] => 300 [crop] => )
[large] => Array ( [width] => 1024 [height] => 1024 [crop] => )
)
[/php]

So, I used array_keys() function to get the keys of the second array, and ran a foreach loop on the value returned by array_keys, and unset the elements of first array

[php]

$key_names= array_keys ( $array2 );
foreach( $key_names as $key ){
unset($array2[$key]);
}
[/php]