Hello Readers, Today we discussed about How to add custom page into admin dashboard and pagination into WP_Query.
If you want to add custom menu page into your admin panel you can create a plugin or write the code into your function.php file.
You can use my below code and paste into your function.php file and the new menu appears into your admin panel.
add_action( 'admin_menu', 'register_newpage' );
function register_newpage(){
add_menu_page('User Mail Record', 'User Mail Record', 'administrator','emailpage', 'email');
remove_menu_page('email');
}
Also you need "email function" to write your code into it.
In the below function we will create a user detail listing with custom query and custom pagination.
Just copy the code and paste below above function.
function email()
{
global $wpdb;
$pagenum = isset( $_GET['pagenum'] ) ? absint( $_GET['pagenum'] ) : 1;
$limit = 2; // number of rows in page
$offset = ( $pagenum - 1 ) * $limit;
$total = $wpdb->get_var( "SELECT COUNT('id') FROM {$wpdb->prefix}email" );
$num_of_pages = ceil( $total / $limit );
$entries = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}email LIMIT $offset, $limit" );
?>
<style>
.userlist{}
.pagination{width:90%;}
table{border-collapse:collapse;margin-top:80px;}
table tr td, table tr th{text-align: center;padding:10px 0}
table tr .heading{font-size: 20px;padding:15px 0;}
</style>
<div class="userlist">
<table width="90%" border="1" align="center">
<thead>
<tr>
<th colspan="6" class="heading">User Detail Listing</th>
</tr>
<tr>
<th width="5%">SNo.</th>
<th width="10%">Frist Name</th>
<th width="10%">Last Name</th>
<th width="20%">Email Address</th>
<th width="35%">Message</th>
<th width="15%">Date</th>
</tr>
</thead>
<tbody>
<?php $i=1; foreach($entries as $email){ ?>
<tr>
<td><?php echo $i+$offset;?></td>
<td><?php echo $email->frist_name;?></td>
<td><?php echo $email->last_name;?></td>
<td><?php echo $email->email_address;?></td>
<td><?php echo $email->message;?></td>
<td><?php echo $email->created;?></td>
</tr>
<?php $i++; } ?>
</tbody>
<tfoot>
<tr>
<th width="5%">SNo.</th>
<th width="10%">Frist Name</th>
<th width="10%">Last Name</th>
<th width="20%">Email Address</th>
<th width="35%">Message</th>
<th width="15%">Date</th>
</tr>
</tfoot>
</table>
<div class="pagination">
<?php
$page_links = paginate_links( array(
'base' => add_query_arg( 'pagenum', '%#%' ),
'format' => '',
'prev_text' => __( '«', 'text-domain' ),
'next_text' => __( '»', 'text-domain' ),
'total' => $num_of_pages,
'current' => $pagenum
) );
if ( $page_links ) {
echo '<div class="tablenav"><div class="tablenav-pages">' . $page_links . '</div></div>';
}
?>
</div>
</div>
<?php
}
I hope it helps you :) .
0 Comment(s)