Wordpress plugin development
Hi friends this is my very first tutorial in wordpress plugin development. In this tutorial we are going to create an login/logout widget as a plugin.
step 1:
Create a folder in wp-content/plugins/ and name it as whatever u want. But it should represent your plugin name, here i'm going to create an folder ' ktm-wp-login '. Now the folder will be in wp-content/plugins/ktm-wp-login.
step 2:
Create a file ktm-wp-login.php inside the ktm-wp-login folder and write the following code on it.
step 3:
For creating this plugin we need the following functions and hooks:
-wp_loginout()
-wp_register()
-register_widget()
and an action ' widgets_init '
Lets start,
In order to register our widget we need a function register_widget()
You can download this plugin as .doc format.
To know more about writing plugin visit codex Writing a plugin page
step 1:
Create a folder in wp-content/plugins/ and name it as whatever u want. But it should represent your plugin name, here i'm going to create an folder ' ktm-wp-login '. Now the folder will be in wp-content/plugins/ktm-wp-login.
step 2:
Create a file ktm-wp-login.php inside the ktm-wp-login folder and write the following code on it.
/*
Plugin Name: ktm Wordpress Login
Description: With this plugin we can login or logout.
Author:Karthi
Version:1.0
Author URI:http://techpointt.wordpress.com
Plugin URI:http://techpointt.wordpress.com
*/
step 3:
For creating this plugin we need the following functions and hooks:
-wp_loginout()
-wp_register()
-register_widget()
and an action ' widgets_init '
Lets start,
In order to register our widget we need a function register_widget()
<?php
add_action('widgets_init','ktm_register_widget');
function ktm_register_widget(){
register_widget('ktm_login_widget');
}
//Check whether the class is already exist or not.
if(!class_exists('ktm_login_widget')){
//Extend our class with core class WP_Widget
class ktm_login_widget extends WP_Widget{
//Initializing the widget with constructor function
function ktm_login_widget(){
$widget_opts = array(
'classname' => 'ktm_login_widget', //class name for our widget
'description'=>'login/logout' //description will be shown in dashboard active widget's box
);
//Wp_Widget('id_for_our_widget','title_for_our_widget_in_dashboard','options')
$this->WP_Widget('ktm_login_widget_id','Log-in/Log-out',$widget_opts);
}
//the parameter $instance used to save data in wp_options table
/*WP_Widget is a core class uses 3 functions
Form() -to show form after we enable it in sidebar widget
Update() -to update new data entered by us.
Widget() -front end widget content.*/
function form($instance){
//Initializing for the first time
$defaults = array('title'=>'widget title');
//Parse the value with incoming args
$instance = wp_parse_args( (array) $instance, $defaults);
$title = $instance['title'];
//get_field_name() helps to fetch encoded data from wp_options table. We can find the value in ktm_login_widget_id name
echo 'Title: ';
}
function update($new_instance,$old_instance){
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
return $instance;
}
function widget($args, $instance){
if(is_user_logged_in()){
global $current_user;
$user = $current_user->user_login;
}else{
$user = 'Guest';
}
echo $args['before_widget'];
echo $args['before_title'].$instance['title'].' '.$user.$args['after_title'];
echo "
<ul>
<li>".wp_loginout(get_permalink(),true)."</li>
<li>".wp_register()."</li>
</ul>";
echo $args['after_widget'];
}
}
}
?>
You can download this plugin as .doc format.
To know more about writing plugin visit codex Writing a plugin page
Comments
Post a Comment