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.
edit-events.php
<div class="container card" style="margin-top:30px; background:wheat">
<?php
global $wpdb;
$id = $_GET['id'];
$result = $wpdb->get_results("SELECT * FROM wp_events WHERE id = $id");
foreach ($result as $key => $print) {
$title = $print->title;
$date = $print->edate;
$cost = $print->cost;
}
?>
<form action="admin.php?page=event-update" method="post">
<h3><b>Update Events Form</b></h3>
<hr/>
<input type="hidden" name="id" value="<?php echo $id; ?>">
<div class="form-group">
<label for="name">Event Title:</label>
<input type="name" class="form-control" id="name" name="etitle" value="<?php echo $title; ?>">
</div>
<div class="form-group">
<label for="edate">Event date:</label>
<input type="date" class="form-control" id="edate" name="edate" value="<?php echo $date; ?>">
</div>
<div class="form-group">
<label for="number">Cost:</label>
<input type="number" class="form-control" id="ecost" name="ecost" value="<?php echo $cost; ?>">
</div>
<button type="submit" class="btn btn-default">Update</button>
</form>
</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';
}
update-event.php
<?php
global $wpdb;
$id = $_POST['id'];
$title = $_POST['etitle'];
$cost = $_POST['ecost'];
$date = $_POST['edate'];
$wpdb->query("UPDATE wp_events SET title='$title',
cost='$cost', edate='$date' WHERE id=$id");
echo "<script>location.replace('admin.php?page=event-edit&id=$id');</script>";
?>