Create a simple wordpress plugin from scratch

Creating a wordpress plugin is very simple. Here I show you step by step of creating a simple wordpress plugin from scratch.

I will show  simple wordpress plugin for a paypal donate button.

First task in creating a WordPress Plugin is to choice a name for your wordpress plugin. Here our plugin name is paypal-donate .

Plugin Files:

Now you have to create a PHP file derived from your chosen Plugin name . Here I create a file named paypal_donate.php

Plugin Header :

Now Create the header section of your plugin. This section contain plugin information and show this information on wordpress plugin manage panel.

<?php
        /*
        Plugin Name: Paypal Donate Widget
        Plugin URI:https://github.com/salayhin/Wordpress-Plugins/tree/master/paypal-donate
        Description: Paypal Donate button for wordpress
        Author: sirajus salayhin
        Version: 0.0.1
        Author URI: http://salayhin.com
        */
?>

Lisence:
After this header section you should add a license for your wordpress plugin. Here I add GPL2 license used by wordpress.

<?php
/*  Copyright Md. Sirajus Salayhin  (email : salayhin@gmail.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
*/
?>

Main plugin section :
First I add a hook for initialization the widget.

add_action('widgets_init', 's_paypaldonate_load_widgets');

Then create a function to call the hook. You have to careful here to choice your function name. Function name must be unique. For this I use a prefix with all function.

function s_paypaldonate_load_widgets()
{
    register_widget('S_Paypaldonate_Donate');
}

Create Widget
Now create a class for widget.

class S_Paypaldonate_Donate extends WP_Widget
{

Constructor for widget class :

function S_Paypaldonate_Donate()
    {

        $widget_ops = array('classname' => 'paypaldonate', 'description' => __('Paypal donate button with custom settings', 'paypaldonate'));

        $control_ops = array('width' => 300, 'height' => 350, 'id_base' => 'paypal-donate-widget');

        $this->WP_Widget('paypal-donate-widget', __('Paypal Donate', 'paypaldonate'), $widget_ops, $control_ops);
    }

Main widget area for show the paypal button :

function widget($args, $instance)
    {
        extract($args);

        $pp_email = apply_filters('widget_pp_email', $instance['pp_email']);
        $sname = $instance['sname'];

        $show_cc = isset($instance['show_cc']) ? $instance['show_cc'] : false;

        echo $before_widget;

        //	echo $before_title . "" . $after_title;

        if ($show_cc) {

            echo '
<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_donations">
<input type="hidden" name="business" value="' . $pp_email . '">
<input type="hidden" name="lc" value="US">
<input type="hidden" name="item_name" value="' . $sname . '">
<input type="hidden" name="no_note" value="0">
<input type="hidden" name="currency_code" value="USD">
<input type="hidden" name="bn" value="PP-DonationsBF:btn_donateCC_LG.gif:NonHostedGuest">
<input type="image" src="https://www.paypal.com/en_US/i/btn/btn_donateCC_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
<img alt="" border="0" src="https://www.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1" align="center">
</form>
';

        } else {

            echo '
<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_donations">
<input type="hidden" name="business" value="' . $pp_email . '">
<input type="hidden" name="lc" value="US">
<input type="hidden" name="item_name" value="' . $sname . '">
<input type="hidden" name="no_note" value="0">
<input type="hidden" name="currency_code" value="USD">
<input type="hidden" name="bn" value="PP-DonationsBF:btn_donate_LG.gif:NonHostedGuest">
<input type="image" src="https://www.paypal.com/en_US/i/btn/btn_donate_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
<img alt="" border="0" src="https://www.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1"></form>
';

        }
        echo $after_widget;

    }

Widget Settings Form :
This section contain form of the settings page.

function form($instance)
    {

        $defaults = array('pp_email' => __('example@example.com', 'paypaldonate'), 'sname' => __('Your service name', 'paypaldonate'), 'show_cc' => true);
        $instance = wp_parse_args((array)$instance, $defaults); ?>

    <p>
        <label for="<?php echo $this->get_field_id('pp_email'); ?>"><?php _e('Paypal email:', 'hybrid'); ?></label>
        <input id="<?php echo $this->get_field_id('pp_email'); ?>"
               name="<?php echo $this->get_field_name('pp_email'); ?>" value="<?php echo $instance['pp_email']; ?>"
               style="width:100%;"/>
    </p>

    <p>
        <label
            for="<?php echo $this->get_field_id('sname'); ?>"><?php _e('Service Name/Organization:', 'paypaldonate'); ?></label>
        <input id="<?php echo $this->get_field_id('sname'); ?>" name="<?php echo $this->get_field_name('sname'); ?>"
               value="<?php echo $instance['sname']; ?>" style="width:100%;"/>
    </p>

    <p>
        <label
            for="<?php echo $this->get_field_id('show_cc'); ?>"><?php _e('Display credit cards?', 'paypaldonate'); ?></label>
        <input class="checkbox" type="checkbox" <?php checked($instance['show_cc'], true); ?>
               id="<?php echo $this->get_field_id('show_cc'); ?>"
               name="<?php echo $this->get_field_name('show_cc'); ?>"/>

    </p>

    <?php
    }

Update Form :
Almost done. here is the update method for settings.

function update($new_instance, $old_instance)
    {
        $instance = $old_instance;

        $instance['pp_email'] = strip_tags($new_instance['pp_email']);
        $instance['sname'] = strip_tags($new_instance['sname']);

        $instance['show_cc'] = $new_instance['show_cc'];

        return $instance;
    }

Now you can upload this plugin from wordpress admin panel and activate.

Here is full code for this plugin.

<?php
/*
Plugin Name: Paypal Donate Widget
Plugin URI:https://github.com/salayhin/Wordpress-Plugins/tree/master/paypal-donate
Description: Paypal Donate button for wordpress
Author: sirajus salayhin
Version: 0.0.1
Author URI: http://salayhin.com
*/
/*  Copyright Md. Sirajus Salayhin  (email : salayhin@gmail.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
*/

add_action('widgets_init', 's_paypaldonate_load_widgets');

function s_paypaldonate_load_widgets()
{
    register_widget('S_Paypaldonate_Donate');
}

class S_Paypaldonate_Donate extends WP_Widget
{
    function S_Paypaldonate_Donate()
    {

        $widget_ops = array('classname' => 'paypaldonate', 'description' => __('Paypal donate button with custom settings', 'paypaldonate'));

        $control_ops = array('width' => 300, 'height' => 350, 'id_base' => 'paypal-donate-widget');

        $this->WP_Widget('paypal-donate-widget', __('Paypal Donate', 'paypaldonate'), $widget_ops, $control_ops);
    }

    function widget($args, $instance)
    {
        extract($args);

        $pp_email = apply_filters('widget_pp_email', $instance['pp_email']);
        $sname = $instance['sname'];

        $show_cc = isset($instance['show_cc']) ? $instance['show_cc'] : false;

        echo $before_widget;

        //	echo $before_title . "" . $after_title;

        if ($show_cc) {

            echo '
                    <form action="https://www.paypal.com/cgi-bin/webscr" method="post">
                    <input type="hidden" name="cmd" value="_donations">
                    <input type="hidden" name="business" value="' . $pp_email . '">
                    <input type="hidden" name="lc" value="US">
                    <input type="hidden" name="item_name" value="' . $sname . '">
                    <input type="hidden" name="no_note" value="0">
                    <input type="hidden" name="currency_code" value="USD">
                    <input type="hidden" name="bn" value="PP-DonationsBF:btn_donateCC_LG.gif:NonHostedGuest">
                    <input type="image" src="https://www.paypal.com/en_US/i/btn/btn_donateCC_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
                    <img alt="" border="0" src="https://www.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1" align="center">
                    </form>
                    ';

        } else {

            echo '
                    <form action="https://www.paypal.com/cgi-bin/webscr" method="post">
                    <input type="hidden" name="cmd" value="_donations">
                    <input type="hidden" name="business" value="' . $pp_email . '">
                    <input type="hidden" name="lc" value="US">
                    <input type="hidden" name="item_name" value="' . $sname . '">
                    <input type="hidden" name="no_note" value="0">
                    <input type="hidden" name="currency_code" value="USD">
                    <input type="hidden" name="bn" value="PP-DonationsBF:btn_donate_LG.gif:NonHostedGuest">
                    <input type="image" src="https://www.paypal.com/en_US/i/btn/btn_donate_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
                    <img alt="" border="0" src="https://www.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1"></form>
                    ';

        }
        echo $after_widget;

    }

    function form($instance)
    {

        $defaults = array('pp_email' => __('example@example.com', 'paypaldonate'), 'sname' => __('Your service name', 'paypaldonate'), 'show_cc' => true);
        $instance = wp_parse_args((array)$instance, $defaults); ?>

    <p>
        <label for="<?php echo $this->get_field_id('pp_email'); ?>"><?php _e('Paypal email:', 'hybrid'); ?></label>
        <input id="<?php echo $this->get_field_id('pp_email'); ?>"
               name="<?php echo $this->get_field_name('pp_email'); ?>" value="<?php echo $instance['pp_email']; ?>"
               style="width:100%;"/>
    </p>

    <p>
        <label
            for="<?php echo $this->get_field_id('sname'); ?>"><?php _e('Service Name/Organization:', 'paypaldonate'); ?></label>
        <input id="<?php echo $this->get_field_id('sname'); ?>" name="<?php echo $this->get_field_name('sname'); ?>"
               value="<?php echo $instance['sname']; ?>" style="width:100%;"/>
    </p>

    <p>
        <label
            for="<?php echo $this->get_field_id('show_cc'); ?>"><?php _e('Display credit cards?', 'paypaldonate'); ?></label>
        <input class="checkbox" type="checkbox" <?php checked($instance['show_cc'], true); ?>
               id="<?php echo $this->get_field_id('show_cc'); ?>"
               name="<?php echo $this->get_field_name('show_cc'); ?>"/>

    </p>

    <?php
    }

    function update($new_instance, $old_instance)
    {
        $instance = $old_instance;

        $instance['pp_email'] = strip_tags($new_instance['pp_email']);
        $instance['sname'] = strip_tags($new_instance['sname']);

        $instance['show_cc'] = $new_instance['show_cc'];

        return $instance;
    }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s