create a plagin code


Enough talking it is time to write some code, let’s start with function, which will be responsible for creating additional table in our database and populating it with two records. In this table we will keep information about different bots and their visits number.

function bot_install()
{
global $wpdb;
$table = $wpdb->prefix.”bot_counter”;
$structure = “CREATE TABLE $table (
id INT(9) NOT NULL AUTO_INCREMENT,
bot_name VARCHAR(80) NOT NULL,
bot_mark VARCHAR(20) NOT NULL,
bot_visits INT(9) DEFAULT 0,
UNIQUE KEY id (id)
);”;
$wpdb->query($structure);

// Populate table
$wpdb->query(“INSERT INTO $table(bot_name, bot_mark)
VALUES(‘Google Bot’, ‘googlebot’)”);
$wpdb->query(“INSERT INTO $table(bot_name, bot_mark)
VALUES(‘Yahoo Slurp’, ‘yahoo’)”);
}

so our best shot will be “activate” Plugin Hook, but be careful these one is a bit tricky.

All of WordPress actions are predefined, all except these one. Look at the code:

add_action(‘activate_bot/bots.php’, ‘bot_install’);

Advertisements

Remove cache in cake php use it The Motivation


The Motivation

1. When you work with large projects you can end up with a ton of cache files (models, db cache, etc.) as well as view caching files. When you try removing them with:

cd /app/tmp/cache/models && rm -f cake_*

and

cd /app/tmp/cache/views && rm -f *

.. you can end up with with the “argument list too long” error easily.

2. Also, executing two commands sucks – we are lazy after all. Now you could pull out some fancy bash fun to pipe file names. Have a look at this:

find . -type f | awk ‘!/empty/ {print “rm”, $0}’ | bash

The problem is, when you run this in /app/tmp it will not only remove cache files, but also files in /tmp/sessions, /tmp/logs and so on. If you ask me, the command is complex enough, so no need to add more funny stuff there to take this into account.

(For you peeps who want to see this, I bugged Felix to tell me: find . -type f | awk ‘!/empty$|^.\/logs|^.\/sessions/ {print “rm”, $0}’ | bash)

3. Once you are on windows, you do not have a powerful bash to your side.

I thought a simple call to a CakePHP shell can do the trick as well and doesn’t force you to waste half a minute to remember and type in the proper bash command.

Add The Categories Selector Widget To The PAGE Editor With Predefined Categories Listed?


The following code will add the categories selector widget to the WordPress Page editor interface…

add_action(‘admin_menu’, ‘my_post_categories_meta_box’);
function my_post_categories_meta_box() {
add_meta_box(‘categorydiv’, __(‘Categories’), ‘post_categories_meta_box’, ‘page’, ‘side’, ‘core’);
}

What I would like to do is to figure out how to modify the resulting category listing so that it only contains a predefined list of hard coded categories that I define. Since I’m adding this via my custom theme, it will only appear on the page editor when my theme is active on the site. And I have some specific “handler” categories that my theme installs into the site and later uses to determine layout elements. I only want these specific categories to be listed in this particular instance of the categories widget.

Using AJAX and CakePHP to dynamically populate select form fields


Today, I struggled a bit to get a select field to be dynamically populated with values. The values are depending on what choice has been made earlier in the form in combination with CakePHP.

Before we get into things I must give credit where credit is due, this article helped me a lot, although since it was based on CakePHP 1.2 things were a bit outdated, so I had to adjust it here and there.

Time to get to the point. I’m assuming that you’re already familiar with AJAX and are just trying to integrate it nicely into your Cake app. I won’t be getting into AJAX itself, so if you’re not familiar with that, I suggest reading up on it first.

Let’s say you have a form with 2 drop down menus. For example, we would like to populate a dropdown with a list of chapters whenever a book is selected from the first dropdown. We want the chapters dropdown to get re-populated with options if the book were to change. In order to achieve this, we can use CakePHP’s built-in Ajax helper, which saves us loads of time. You can easily call the Ajax helper by putting this line in the top section of your controller:

var $helpers = array(‘Ajax’);

That will make the Ajax helper available to all the views belonging to this controller’s actions. Now, on top of that, we would like our views to be “expecting” AJAX calls and stand by for them. To do that, you can use CakePHP’s RequestHandler component, which can be included in your controller like this:

var $components = array(‘RequestHandler’);

Now, let’s say we would like to give users the ability to add a synopsis for a chapter of a book. Depending on what book they pick, a list of chapters for that book should be presented. Let’s assume the models are associated with the relation: Book hasMany Chapter. Now let’s take a look at what the view for such a page might look like.

Add synopsis

Form->create(‘Book’);
echo $this->Form->input(‘Book.id’, array(‘label’ => ‘Book’, ’empty’ => ‘– Pick a book –‘));
echo $this->Form->input(‘Book.chapter_id’, array(‘label’ => ‘Chapter’, ’empty’ => ‘– Pick a chapter –‘));
echo $this->Form->input(‘Chapter.synopsis’);
echo $this->Form->end(‘Submit synopsis’);
?>