We’re going to dive into the world of WordPress and learn how to create a custom CRUD (Create, Read, Update, Delete) system in the WordPress admin area without relying on plugins. This tutorial will walk you through creating your own tables and managing data within them.
all-events.php
<div class="container" style="margin-top:30px">
<table class="table table-striped" style="background:#f7f7f7">
<tr>
<td>S.No</td>
<td>Title</td>
<td>Date</td>
<td>Cost</td>
<td>Action</td>
</tr>
<!-- loop start -->
<?php
global $wpdb;
$result = $wpdb->get_results("SELECT * FROM wp_events");
foreach ($result as $key => $print) {
?>
<tr>
<td width='25%'><?php echo $key+1; ?></td>
<td width='25%'><?php echo $print->title; ?></td>
<td width='25%'><?php echo $print->edate; ?></td>
<td width='25%'><?php echo $print->cost; ?></td>
<td width='25%'>
<a href='admin.php?page=event-edit&id=<?php echo $print->id; ?>'>
<button class='btn btn-success btn-sm' type='button'>Edit</button></a>
<a href='admin.php?page=crud.php&del=<?php echo $print->id; ?>'>
<button class='btn btn-danger btn-sm' type='button'>DELETE</button>
</a>
</td>
</tr>
<?php
}
?>
<!-- loop end -->
</table>
</div>
functions.php
// navigation
function my_admin_menu() {
add_menu_page( __( 'My Events', 'My Events' ),
__( 'Events', 'My Events' ),
'manage_options', 'my-events',
'all_event_fun',
'dashicons-admin-site', 5 );
add_submenu_page('my-events', __('My Events',
'my-events'), __('All Events', 'my-events'),
'manage_options', 'my-events',
'all_event_fun');
add_submenu_page('my-events', __('Add event',
'add-event'), __('Add Event', 'add-event'),
'manage_options', 'add-event',
'add_event_fun');
add_submenu_page(' ', __('Submit event',
'event-submit'), __('Submit Event', 'event-submit'),
'manage_options', 'event-submit',
'submit_event_fun');
add_submenu_page(' ', __('Event Edit',
'event-edit'), __('Event Edit', 'event-edit'),
'manage_options', 'event-edit',
'edit_event_fun');
add_submenu_page(' ', __('Event Update',
'event-update'), __('Event Update', 'event-update'),
'manage_options', 'event-update',
'update_event_fun');
}
add_action( 'admin_menu', 'my_admin_menu' );
function all_event_fun(){
include_once get_theme_file_path().'/events/all-events.php';
}
function add_event_fun(){
include_once get_theme_file_path().'/events/add-events.php';
}
function submit_event_fun(){
include_once get_theme_file_path().'/submit-events.php';
}
function edit_event_fun(){
include_once get_theme_file_path().'/events/edit-events.php';
}
function update_event_fun(){
include_once get_theme_file_path().'/events/update-events.php';
}