Creating wordpress plugin (LOGIN VIEWER)

For creating wordpress plugin. I will show how to create a very simple one..

The purpose of this plugin by the way is to create a login viewer for the site created in wordpress.

Create a file then name like loginviewer.php

Insert the necessary template label for that file at the very top of the page..


/*
Plugin Name: Login Viewer
Plugin URI: http://angelohere.wordpress.com/plugin
Description: View the User login logs
Version: 1.0
Author: ngelMat
Author URI: http://angelohere.wordpress.com
License: A "Slug" license name e.g. GPL2
*/

<?php
include_once 'includes/db_setup.php';
$objLoginViewer = new LoginViewer();

function admin_init_loginviewer() {
    register_setting('loginviewer', 'log');
}

function admin_activate_loginviewer() {
    global $objLoginViewer;
    $objLoginViewer = new LoginViewer();
    $objLoginViewer->createDatabase();    
}

function admin_deactivate_loginviewer() {
    global $objLoginViewer;
    $objLoginViewer = new LoginViewer();
    $objLoginViewer->dropDatabase();    
}

function admin_menu_loginviewer() {    
    add_options_page('Login Viewer', 'Login Viewer', 'level_8', 
'loginviewer', 
'options_page_loginviewer');

}

function options_page_loginviewer() {    
    include_once WP_PLUGIN_DIR . '/loginviewer/options.php';;
}

function postUserLogged($user) {
    global $objLoginViewer;
    $objLoginViewer->postLoggedIn($user);
}

function postUserLoggedOut() {
    global $objLoginViewer;    
    date_default_timezone_set('EST');
    $dateTimeEnded = date(' Y:m:d H:i:s A ');
    $_SESSION['logout_time'] = $dateTimeEnded;
    $objLoginViewer->postLoggedOut();
}

register_activation_hook(__FILE__, 'admin_activate_loginviewer');
register_deactivation_hook(__FILE__, 'admin_deactivate_loginviewer');

if (is_admin ()) {
    add_action('admin_init', 'admin_init_loginviewer');
    add_action('admin_menu', 'admin_menu_loginviewer');
}
add_filter('wp_logout', 'postUserLoggedOut');

?>


//in options.php file
//insert this code
<div class="wrap">
<h2>Login Viewer</h2>
    <?php
        include_once 'tableData.php';
    ?>
</div>


//for the database setup 
1. create a folder named includes/ and then create a file there named
db_setup.php

<?php
//session_start();
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
class LoginViewer {

    
    var $table = 'loginlogged';
    var $user_login;
    var $user_email;



    function  __construct() {

    }

    
    function createDatabase () {
        global $table_prefix;
        global $wpdb;

        $this->dropDatabase();
        $query = "CREATE TABLE $table_prefix$this->table
                    (id BIGINT(20) NOT NULL AUTO_INCREMENT,
                    user_login VARCHAR(60) NOT NULL ,
                    user_email VARCHAR(100) NOT NULL ,
                    role VARCHAR(100) NOT NULL ,
                    user_display_name VARCHAR(250) NOT NULL ,
                    login_time DATETIME NOT NULL ,
                    logout_time DATETIME NOT NULL,
                    PRIMARY KEY (id)  );";

        dbDelta($query);

    }

    function dropDatabase() {        
        global $table_prefix;
        global $wpdb;

        $query = "DROP TABLE IF EXISTS $table_prefix$this->table; ";        
        dbDelta($query);
    }


    function postLoggedIn($user) {
        global $table_prefix;
        global $wpdb;
        
        $currentUser = $user;
        date_default_timezone_set('EST');
        $dateTimeStarted = date(' Y:m:d H:i:s A ');        

        if (isset ($currentUser)) {
            if (isset ($currentUser->user_login))
                $postUser['user_login'] = $currentUser->user_login;
            if (isset ($currentUser->user_email))
                $postUser['user_email'] = $currentUser->user_email;
            if (isset ($currentUser->display_name))
                $postUser['display_name'] = $currentUser->display_name;
            if (isset ($currentUser->roles[0]))
                $postUser['role'] = $currentUser->roles[0];
            $postUser['login_time'] = $dateTimeStarted;


        }
        $countUserField = count($postUser);                
        if ($countUserField == 5) {

            $query = "INSERT INTO $table_prefix$this->table
                                (
                                 user_login,
                                 user_email,
                                 role,
                                 user_display_name,
                                 login_time)
                    VALUES (
                            '" . $postUser['user_login'] . "',
                            '" . $postUser['user_email'] . "',
                            '" . $postUser['role'] . "',
                            '" . $postUser['display_name'] . "',
                            '" . $postUser['login_time'] . "')";
            
            $wpdb->query($query);
        }

    }

    function postLoggedOut() {
        global $table_prefix;
        global $wpdb;
        
        $currentUser = wp_get_current_user();        
        if (isset ($currentUser)) {
            if (isset ($currentUser->user_login))
                $postUser['user_login'] = $currentUser->user_login;
        }
        date_default_timezone_set('EST');
        $dateTimeEnded = date(' Y:m:d H:i:s A ');
        $postUser['logout_time'] = $dateTimeEnded;
        
        
        $loggedId = $wpdb->get_var($wpdb->prepare("SELECT ID 
FROM $table_prefix$this->table WHERE user_login = %s ORDER BY id DESC LIMIT 1" , 
$postUser['user_login']));


        $query = "UPDATE $table_prefix$this->table
                    SET logout_time = '" . $postUser['logout_time'] . "'
                    WHERE id = $loggedId ;";
        $wpdb->query($query);
        
    }


    function selectAll () {
        global $table_prefix;
        global $wpdb;

        $query = "SELECT
                      user_login,
                      user_email,
                      role,
                      user_display_name,
                      login_time,
                      logout_time
                    FROM $table_prefix$this->table
                    ORDER BY id desc;";
        $result = $wpdb->get_results($query);
        $tableName = $table_prefix . $this->table;
        if($wpdb->get_var("show tables like '$tableName'") != $tableName) {
            $this->createDatabase();
        }
        
        return $result;
    }


   
}
?>



//Then create tabledata.php file wherein it is the 
script which we will be viewing the data presentation and insert the code below.

<?php
$objLoginViewer = new LoginViewer();
$loginLogs = $objLoginViewer->selectAll();
?>
<style type="text/css">
.loginViewer-logs {
    border: 1px solid #DFDFDF;
    width: 100%;    
}
.loginViewer-logs thead,#loginViewer-logs th  {
    background: #ddd6d6;
    font-weight: bold;
}
.loginViewer-logs #odd {
    background: #fcf1f1;
}
.loginViewer-logs #even {
    background: #fff;
}
.loginViewer-logs td,
.loginViewer-logs td {
    width: 200px;
    text-align: left;
}
#login-dataTitle {    
    color: #21759B;
    text-align: left;
}
#loginlogs-data {
    max-height: 400px;
    overflow: scroll;
}

</style>
<table class="loginViewer-logs" cellspacing="0" cellpadding="2">
    <thead id="login-dataTitle">
        <td>Login Name</td>
        <td>Email</td>
        <td>Role</td>
        <td>Name</td>
        <td>Login Time</td>
        <td>Logout Time</td>
    </thead>
</table>
<div id="loginlogs-data">
<table class="loginViewer-logs">
    <tbody>
        <?php
            $row = 0;
            if (isset ($loginLogs) && count($loginLogs))
            foreach ($loginLogs as $loginLog) {
                $row++;
        ?>
            <?php
                if ($row % 2 == 1) {
            ?>
                    <tr id="odd">
                        <td><?php echo $loginLog->user_login; ?></td>
                        <td><?php echo $loginLog->user_email; ?></td>
                        <td><?php echo $loginLog->role; ?></td>
                        <td><?php echo $loginLog->user_display_name; ?></td>
                        <td><?php
                            if ($loginLog->login_time != '0000-00-00 00:00:00')
                                echo date("m/d/Y g:i:s A",
strtotime($loginLog->login_time)) ;
                            else
                                echo $loginLog->login_time;
                            ?>
                        </td>
                        <td><?php

                            if ($loginLog->logout_time != '0000-00-00 00:00:00')
                                echo date("m/d/Y g:i:s A",
strtotime($loginLog->logout_time)) ;
                            else
                                echo $loginLog->logout_time;
                            ?>
                        </td>
                    </tr>
            <?php
                } else {
            ?>
                    <tr id="even">
                        <td><?php echo $loginLog->user_login; ?></td>
                        <td><?php echo $loginLog->user_email; ?></td>
                        <td><?php echo $loginLog->role; ?></td>
                        <td><?php echo $loginLog->user_display_name; ?></td>
                        <td><?php
                            if ($loginLog->login_time != '0000-00-00 00:00:00')
                                echo date("m/d/Y g:i:s A",
strtotime($loginLog->login_time)) ;
                            else
                                echo $loginLog->login_time;
                            ?>
                        </td>
                        <td><?php

                            if ($loginLog->logout_time != '0000-00-00 00:00:00')
                                echo date("m/d/Y g:i:s A",
strtotime($loginLog->logout_time)) ;
                            else
                                echo $loginLog->logout_time;
                            ?>
                        </td>

                    </tr>
        <?php
                }
        }
        ?>
    </tbody>
</table>
</div>

//And then lastly create a readme.txt file and put some text 
like this one in order to use this kind of loginviewer login plugin

Login viewer

insert this code in the wp-login.php
before redirection..

 if (function_exists('postUserLogged')) {
                    postUserLogged($user);
 }

//And so that is it.
We have created a plugin in order to view login logs of a wordpress site.
Hope this helps to track administration of some changes in the site..

Thanks.

2 thoughts on “Creating wordpress plugin (LOGIN VIEWER)

Leave a Reply

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