[Javascript] jQuery and Chrome extensions

Chrome extensions are usually based on javascript. Nowadays, jQuery is always used/though of when javascript is mentioned. Thus, it’s essential to know how to use jQuery – at least in the popup.

To get familiar with chrome extension development – check this out.

To trigger something when the page is loaded in javascript it’s usually the famous


This, unfortunately, will not work in the popup of your extension. Instead, I used another function

function ext_ready(){

window.onload = ext_ready;

Ofcourse that is not my solution – but I read it somewhere long time ago that I forgot the resource. Probably it’s on stackoverflow somewhere.

[PHP] Validation

Validation is headache. Fortunately, this function makes it better. Sample usage is as follows:

function fields_validation($field){
	$validation_rules = array();
		$slug = $field->slug;
	} else {
		$slug = sanitize_title($field->label);
	$label = $field->label;
	if(intval($field->minLength) != '0'){
		$minLength = intval($field->minLength);
		$validation_rules[] = "length>=$minLength,$slug,$label must be at least $minLength characters long.";
	if(intval($field->maxLength) != '0'){
		$maxLength = intval($field->maxLength);
		$validation_rules[] = "length<=$maxLength,$slug,$label must be $maxLength characters long maximum.";
		$validation_rules[] = "valid_email,$slug,Please enter a valid email address.";
	return $validation_rules;

and then call the function itself

$validation_rules = fields_validation($field);
$errors = validateFields($_POST, $validation_rules);

$errors will be an array. A simple !empty($errors) will check for errors.

The conditions represents the rule that must be true in order not to produce an error. For example, length <= $maxLength means that the length must be less than max length to pass the check. $slug is the name of the form’s field.

[WordPress] Plugin’s Menu

To create a menu for a custom plugin, you can use the function add_menu_page. Here is a sample:

add_menu_page('Title of the page', 'The text to appear in menu', 'create_users', 'a-unique-slug-to-avoid-conflicts', 'function-that-renders-the-page-to-appear');

This will create a main menu item. To create a sub menu item under the one mentioned above, we can use the add_submenu_page. Here is a sample code:

add_submenu_page('the-slug-you-used-above-in-the-main-menu-item', 'Page title to appear', 'The text to appear in menu', 'create_users', 'another-different-unique-slug', 'function-that-renders-the-page-to-appear' );

The ‘create_users’ is the permission required for this menu item/page to be visible/accessible.

[Cakephp] Basic and simple permissions

For cakephp you can use the AppController to check for permissions and perform other operations that you want to apply for all controllers. One of the operations is using the isAuthorized function to check whether the current user has the permission to use a certain action. A general approach is to define the permissions in an array something like

$applicant = array(
'applications' => array('index', 'edit')

In this scope, the applicant role in my application can perform the ‘index’ and ‘edit’ actions (functions) in the ‘applications’ controller. Next,

			$role = $user['role'];
			if($role == 'admin'){
				// If admin, do everything
				return true;
			} else if($role == 'applicant'){
				// If applicant, check the applicant array
				if(array_key_exists(strtolower($this->name), $applicant)){
					if(in_array(strtolower($this->action), $applicant[strtolower($this->name)])){
						return true;
					} else {
						return false;
				} else {
					return false;
		} else {
			return false;

$this->name returns the current controller. $this->action returns the current action the user is trying to view.

[Cakephp] Save multiple associated data

In a one to many relationship, say a question to answers, and I am using javascript to add some answers to the question. It’s best to add the answer fields as follows in the views (replace 0 with a counter variable or something)

<input name="data[Answer][0][answer]">

and make use of the save associated in the controller’s function that handles adding the question as