WordPress plugin Hooks: Activation and Deactivation

Ok, so now I’ve got the beginnings of a plugin.

Time to learn about hooks!

However, first, notice that I’ve added text related to the actual license. Turns out that’s a proper practice, so I’ve added it as follows:

<?php
/*
Plugin Name: My Plugin
Plugin URI: http://blog.kennyray.com
Description: This is a plugin
Version: 1.0
Author: Kenny Ray
Author URI: http://blog.kennyray.com
License: GPL2
*/

/*  Copyright 2013  Kenny Ray  (email : kenny@blog.kennyray.com)

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License, version 2, as 
    published by the Free Software Foundation.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/
?>

Ok, so now what I want to do is make the plugin do something.

The first thing that the user is going to do is activate the plugin. So I need a “hook” for that:

<?php
/*
Plugin Name: My Plugin
Plugin URI: http://blog.kennyray.com
Description: This is a plugin
Version: 1.0
Author: Kenny Ray
Author URI: http://blog.kennyray.com
License: GPL2
*/

/*  Copyright 2013  Kenny Ray  (email : kenny@blog.kennyray.com)

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License, version 2, as 
    published by the Free Software Foundation.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

function activate_my_plugin(){

}

register_activation_hook( __FILE__, 'activate_my_plugin' );

?>

What I’ve done here is add a function at line 28 that will be called when the user clicks the activate button.

The code on line 32 is the hook. It tells WordPress to call the function at line 28 when the activate button is pressed.

Of course, if the user wants to remove the plugin, I’ll need to provide a way to undo any setup that might be done in the activation handler. That is accomplished with another function and hook:

 

<?php
/*
Plugin Name: My Plugin
Plugin URI: http://blog.kennyray.com
Description: This is a plugin
Version: 1.0
Author: Kenny Ray
Author URI: http://blog.kennyray.com
License: GPL2
*/

/*  Copyright 2013  Kenny Ray  (email : kenny@blog.kennyray.com)

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License, version 2, as 
    published by the Free Software Foundation.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

function activate_my_plugin(){

}

function deactivate_my_plugin(){

}

register_activation_hook( __FILE__, 'activate_my_plugin' );
register_deactivation_hook(__FILE__, 'deactivate_my_plugin' );

?>

On line 32, there is a function which will run upon deactivation. The hook is on line 37.

 

Conclusion

At this point, I have a basic starting point for a plugin. The plugin should activate and deactivate successfully, and if there was code in either function, it would be executed at the appropriate time.

 

API Reference: http://codex.wordpress.org/Function_Reference/register_activation_hookhttp://codex.wordpress.org/Function_Reference/register_deactivation_hook

WordPress plugins: Where to begin

Here is one place you can start if you are looking for basic info on creating a plugin:

http://codex.wordpress.org/Writing_a_Plugin

 

Create a file in the plugins folder. Call it whatever you’d like.

 

Place a comment block at the top like this, filling in the info specific to your plugin:

<?php
/*
Plugin Name: My Plugin
Plugin URI: http://blog.kennyray.com
Description: This is a plugin
Version: 1.0
Author: Kenny Ray
Author URI: http://blog.kennyray.com
License: GPL2
*/
?>

At this point, you will see your Plugin in the plugins section of the site admin.