Monday, September 16, 2013

WORDRESS PLUGIN TO SHOW TABLE LISTING WITH PAGING IN ADMIN SECTION (WP-ADMIN)

Hiiii, if you are developing a wordpress plugin then you might encounter a problem of how to show your custom data in wordpress admin listing.
Here is the code to show a listing of any wordpress table in WP admin section with a delete facility by making a plugin .
wordpress plugin

Two fIles are used to implement these functionality :
  • Plugin-name.php : This file is the main plugin file whose name is same as the name of your plugin.
  • Listing.php : In this file the records of the table are fetch from the database, which you want to  display in your admin section.
Below is the code in both of the files mentioned above :

Plugin-name.php :

<?php
/*
Plugin Name: Plugin-name
Description: Your Description
*/
?>
<link rel=”stylesheet” href=”<?php echo $plugins_url;?>/nhscomment/css/jquery-ui.css” />
<script src=”<?php echo $plugins_url;?>/nhscomment/js/jquery-1.9.1.min.js” type=”text/javascript”></script>
<script src=”<?php echo $plugins_url;?>/nhscomment/js/jquery-ui-1.10.2.custom.js” type=”text/javascript”></script>
<?php
global $wpdb;
add_action(‘admin_menu’, ‘menu_page’);
function menu_page() {
$page_title = ‘Title of page’;
$menu_title = ’tiltle that shows in menu’;
$capability = ‘manage_options’;
$menu_slug = ‘seo friendly url’;
$function = ‘table_listings’;  //function to call
add_menu_page($page_title, $menu_title, $capability, $menu_slug, $function);
}
function table_listings() {
global $wpdb;
require_once(dirname(__FILE__).’/includes/listing.php’);
}
add_filter( ‘wp_nav_menu_items’, ‘menu_item’, 10, 2 );
function menu_item ( $items, $args ) {
if (is_single() && $args->theme_location == ‘primary’) {
$items .= ‘<li>Show whatever</li>’;
}
return $items;
}
if($_GET['comment_id'])
{
$record_id=$_GET['record_id'];
$delete_sql=”delete from “.$wpdb->prefix.”table_name where id=’”.$record_id.”‘”;
$wpdb->query( $delete_sql );
}
?>

Listing.php :

This section i have developed for comment section :
<script>
$(function() {
$( “.comment” ).tooltip();
});
</script>
<div id=”admin_wpfes”>
<h3>Comment Lists</h3>
<?php
$pagenum = isset( $_GET['pagenum'] ) ? absint( $_GET['pagenum'] ) : 1;
$limit = 10;
$offset = ( $pagenum – 1 ) * $limit;
$total = $wpdb->get_var( “SELECT COUNT(`id`) FROM “.$wpdb->prefix.”table_name” );
$num_of_pages = ceil( $total / $limit );
$page_links = paginate_links( array(
‘base’ => add_query_arg( ‘pagenum’, ‘%#%’ ),
‘format’ => ”,
‘prev_text’ => __( ‘&laquo;’, ‘aag’ ),
‘next_text’ => __( ‘&raquo;’, ‘aag’ ),
‘total’ => $num_of_pages,
‘current’ => $pagenum
) );
if ( $page_links ) {
echo ‘<div><div style=”margin: 1em 0″>’ . $page_links . ‘</div></div>’;
}
echo ‘</div>’;
if ($comments = $wpdb->get_results(“SELECT * FROM “.$wpdb->prefix.”table_name ORDER BY id DESC LIMIT “.$offset.”,”.$limit)) {
$comment_no=0;
$url=”http://&#8221;.$_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
?>
<table>
<thead>
<tr>
<th> Table Headers</th>
</tr>
</thead>
<tbody>
<?php
$url = $url . ‘&amp;record_id=’;
foreach ($comments as $comment) {
if ($comment_no&1) {
echo “<tr class=\”alternate\”>”;
} else {
echo “<tr>”;
}
$comment_no=$comment_no+1;
echo “<td>$comment->username</td>”;
echo “<td>$comment->email</td>”;
echo “<td class=’comment’ style=’cursor:pointer;’ title=’”.$comment->comment.”‘>”;
echo substr($comment->comment,0,50).”…”;
//echo “<span class=’comment’ style=’display:none;’>$comment->comment</span>”;
echo “</td>”;
echo “<td>$comment->star</td>”;
echo “<td>$comment->date_from</td>”;
echo “<td>$comment->date_to</td>”;
echo “<td>$comment->added_on</td>”;
echo “<td><a href=\”$url$comment->id\” onclick=\”if(confirm(‘Are you sure you want to delete user with ID $comment->id?’)) return; else return false;\”>Delete</a></td>”;
echo “</tr>”;
}
?>
</tbody>
</table>
<?php
}
?>
</div>
Hope you like this post……..Please Comment....!!!!!!
Originally posted at Mycodestock.


Tags: , ,

0 Responses to “WORDRESS PLUGIN TO SHOW TABLE LISTING WITH PAGING IN ADMIN SECTION (WP-ADMIN)”

Post a Comment

© 2013 MyCodeStock. All rights reserved.
Designed by SpicyTricks