How to create WordPress 3 Custom Plugin for dummies


Creating custom plugin in WordPress 3.2 is easy and it will fast if we do on the right way. So, what we need to know as newbie / n00b to build a custom wordpress plugin ? First, you should read : http://codex.wordpress.org/Writing_a_Plugin.

Now you have insight to build a plugin on WordPress. I will guide you to create wordpress plugin that follow WordPress Guidelines. We will create plugin called “writter”.

Create folder on “/wp-content/plugins” called “writter”. Go to folder and create file called “writter.php”

1
2
cd your-wordpress-path/wp-content/plugins/
mkdir writter

Now we ready to create plugins in WordPress 3 :

1. Standar plugin Information
We start create a standar plugin information on writter.php.

1
2
3
<?php <!–/* Plugin Name: Writter Plugin URI: http://wpscale.com Description: Build custom writter wordpress plugin Version: 1.0 Author: Yodi Author URI: http://yodi.me License: A "Slug" license name e.g. GPL2  */ /* This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. 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 Street, Fifth Floor, Boston, MA  02110-1301, USA. */–>
define(‘WRITTER_VERSION’, ‘1.0’);
define(‘WRITTER_PLUGIN_URL’, plugin_dir_url( __FILE__ )); ?>

When you go to WordPress Administrator -> Installed Plugins, you will see this plugin show up. Start activated it. After activating this plugin, when we update the plugin, we can see the changes on site immediatelly.

2. WordPress Action Hooks
Plugin purpose is to customize, modify or updating default action in WordPress. For instance, we want to automatically tweets article after it published. We should create plugin that create a tweets after “publish action” executed. This publish action is include in WordPress Action Hooks. You can read the full list on http://codex.wordpress.org/Plugin_API/Action_Reference.

Now, i will do action “Template actions” on wp_footer. This hooks will execute when the template calls the wp_footer function. For instance, we need to add value on WordPress footer on plugins. Edit “writter.php” and add this after standard information :

1
2
3
4
5
6
7
/* Example modify wp_footer from plugin */
function writter_footer() {
    echo ‘<div id="site-generator">
        <strong>This is result from writter plugins!</strong>
        </div>’;
}
add_action(‘wp_footer’,’writter_footer’);

Save it and open your wordpress page. You should see “This is result from writter plugins!” on the bottom pages. Cool huh?

3. Adding Admin Menu
Said we want to enable and disable “This is result from writter plugins!” on bottom page from admin menu. You should read http://codex.wordpress.org/Adding_Administration_Menus for detail explanation.

We create writter options page on admin menu by :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/* Adding admin menu */
add_action(‘admin_menu’,’writter_menu’);

function writter_menu() {
    add_options_page(‘Writter Options’,’Writter’,’manage_options’,
        ‘writter-url-id’,’writter_options’);
}

/* Plugin menu options page */
function writter_options() {
    if(!current_user_can(‘manage_options’)) {
        wp_die( __("You don’t have sufficient permission to access."));
    }

    echo ‘<div class="wrap">’;
    echo ‘<p>Here the options page will added later’;
    echo ‘</div>’;
}

Save and login into WordPress Admin and see Settings menu. You should see “Writter” there. Click it and you will see option page and “writter-url-id” as identifier.

4. Create and Saving Plugin Form
We want to set enable and disable function from Admin menu through form. We need to add form, handle submited data and saving into database. WordPress has wp_options table to handle some trivial value.
Update your “writter_options” function by :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
* Plugin menu options page */
function writter_options() {
    if(!current_user_can(‘manage_options’)) {
        wp_die( __("You don’t have sufficient permission to access."));
    }

    /* Variable and saving method on wp_options table */
    $options_name = ‘writter_status’;
    $writter_value = get_option($options_name);

    /* Handle POST */
    if($_POST) {
        $writter_value = $_POST[‘writter_status’];
        update_option($options_name, $writter_value);
        unset($_POST);
    }

    ?>
    <div class="wrap">
        <form name="form-writter" method="post" action="">
        <p>
            <h2><?php _e(‘Footer configuration’,’Writter’); ?></h2>
            <input type="text" name="writter_status" value="<?php
            echo $writter_value;?>" size="20" />
        </p>

        <p class="submit">
            <input id="submit" type="submit" name="submit" class="button-primary"
            value="<?php echo esc_attr_e(‘Save changes’);?>" />
        </p>

        </form>
    </div>
    <?php
}

Now check your “Writter” admin menu. You should see form. Try input “yes” and submit. Your data will saved into WordPress “wp_options”.

Codex that may help you while developing a plugin :
http://codex.wordpress.org/Determining_Plugin_and_Content_Directories
http://codex.wordpress.org/Function_Reference
http://codex.wordpress.org/Plugin_API


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.