WordPress Plugin crud operation Development code

Here’s an example code for a basic WordPress plugin that performs CRUD (Create, Read, Update, Delete) operations on a custom database table:

<?php
/*
Plugin Name: My Custom Plugin
Description: A custom plugin for WordPress
Version: 1.0
Author: Your Name
*/

// Create a custom database table
function create_custom_table() {
global $wpdb;
$table_name = $wpdb->prefix . ‘my_custom_table’;
$charset_collate = $wpdb->get_charset_collate();
$sql = “CREATE TABLE $table_name (
id int(11) NOT NULL AUTO_INCREMENT,
name varchar(50) NOT NULL,
email varchar(50) NOT NULL,
PRIMARY KEY (id)
) $charset_collate;”;
require_once( ABSPATH . ‘wp-admin/includes/upgrade.php’ );
dbDelta( $sql );
}
register_activation_hook( __FILE__, ‘create_custom_table’ );

// Add a menu item for the plugin
function add_custom_menu_item() {
add_menu_page(
‘My Custom Plugin’,
‘My Custom Plugin’,
‘manage_options’,
‘my-custom-plugin’,
‘view_custom_data’
);
}
add_action( ‘admin_menu’, ‘add_custom_menu_item’ );

// Display data from the custom table
function view_custom_data() {
global $wpdb;
$table_name = $wpdb->prefix . ‘my_custom_table’;
$results = $wpdb->get_results( “SELECT * FROM $table_name” );
?>
<div class=”wrap”>
<h1>My Custom Plugin</h1>
<table class=”wp-list-table widefat striped”>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php foreach ( $results as $result ) { ?>
<tr>
<td><?php echo $result->id; ?></td>
<td><?php echo $result->name; ?></td>
<td><?php echo $result->email; ?></td>
<td>
<a href=”<?php echo admin_url( ‘admin.php?page=my-custom-plugin&action=edit&id=’ . $result->id ); ?>”>Edit</a>
<a href=”<?php echo admin_url( ‘admin.php?page=my-custom-plugin&action=delete&id=’ . $result->id ); ?>” onclick=”return confirm(‘Are you sure you want to delete this item?’)”>Delete</a>
</td>
</tr>
<?php } ?>
</tbody>
</table>
<br>
<a href=”<?php echo admin_url( ‘admin.php?page=my-custom-plugin&action=add’ ); ?>” class=”button”>Add New Item</a>
</div>
<?php
}

// Add a new item to the custom table
function add_custom_item() {
global $wpdb;
$table_name = $wpdb->prefix . ‘my_custom_table’;
if ( isset( $_POST[‘submit’] ) ) {
$name = $_POST[‘name’];
$email = $_POST[’email’];
$wpdb->insert(
$table_name,
array(
‘name’ => $name,
’email’ => $email,
)
);
wp_redirect( admin_url( ‘admin.php?page=my-custom-plugin’ ) );
exit;
}
?>
<div class=”wrap”>
<h1>Add New Item</h1>
<form method=”post”>
<table>

<tbody>
<tr>
<td>Name:</td>
<td><input type=”text” name=”name”></td>
</tr>
<tr>
<td>Email:</td>
<td><input type=”email” name=”email”></td>
</tr>
</tbody>
</table>
<br>
<input type=”submit” name=”submit” value=”Add Item” class=”button”>
<a href=”<?php echo admin_url( ‘admin.php?page=my-custom-plugin’ ); ?>” class=”button”>Cancel</a>
</form>
</div>
<?php

}

// Edit an item in the custom table function edit_custom_item() { global $wpdb; $table_name = $wpdb->prefix . ‘my_custom_table’; if ( isset( $_POST[‘submit’] ) ) { $id = $_POST[‘id’]; $name = $_POST[‘name’]; $email = $_POST[’email’]; $wpdb->update( $table_name, array( ‘name’ => $name, ’email’ => $email, ), array( ‘id’ => $id ) ); wp_redirect( admin_url( ‘admin.php?page=my-custom-plugin’ ) ); exit; } $id = $_GET[‘id’]; $result = $wpdb->get_row( “SELECT * FROM $table_name WHERE id = $id” ); ?> <div class=”wrap”> <h1>Edit Item</h1> <form method=”post”> <input type=”hidden” name=”id” value=”<?php echo $result->id; ?>”> <table class=”wp-list-table widefat striped”> <tbody> <tr> <td>Name:</td> <td><input type=”text” name=”name” value=”<?php echo $result->name; ?>”></td> </tr> <tr> <td>Email:</td> <td><input type=”email” name=”email” value=”<?php echo $result->email; ?>”></td> </tr> </tbody> </table> <br> <input type=”submit” name=”submit” value=”Save Changes” class=”button”> <a href=”<?php echo admin_url( ‘admin.php?page=my-custom-plugin’ ); ?>” class=”button”>Cancel</a> </form> </div> <?php }

// Delete an item from the custom table function delete_custom_item() { global $wpdb; $table_name = $wpdb->prefix . ‘my_custom_table’; $id = $_GET[‘id’]; $wpdb->delete( $table_name, array( ‘id’ => $id ) ); wp_redirect( admin_url( ‘admin.php?page=my-custom-plugin’ ) ); exit; }

// Add actions for the plugin’s menu items add_action( ‘admin_menu’, ‘add_custom_menu_item’ ); add_action( ‘admin_init’, ‘add_custom_styles’ );

// Add styles for the plugin’s pages function add_custom_styles() { wp_enqueue_style( ‘custom-style’, plugins_url( ‘style.css’, FILE ) ); } ?>

This code defines the following functions:

– `create_custom_table()` creates a custom database table when the plugin is activated.
– `add_custom_menu_item()` adds a menu item for the plugin to the WordPress dashboard.
– `view_custom_data()` displays the data from the custom table in a table on the plugin’s page.
– `add_custom_item()` adds a new item

to the custom table.

  • edit_custom_item() displays a form to edit an item in the custom table, and updates the table when the form is submitted.
  • delete_custom_item() deletes an item from the custom table.
  • add_custom_styles() adds custom styles to the plugin’s pages.

To create a CRUD operation in your WordPress plugin, you can use these functions as a starting point and modify them as needed. For example, you can add more fields to the table, or change the table’s name. You can also add more functions to handle additional CRUD operations, such as creating or deleting multiple items at once.

When you’re ready to test your plugin, you can activate it in the WordPress dashboard and navigate to the plugin’s page. You should see a table displaying the data from the custom table, and buttons to add, edit, or delete items. When you add or edit an item, the data should be stored in the custom table and displayed in the table on the plugin’s page. When you delete an item, it should be removed from the custom table and the table on the plugin’s page should be updated accordingly.

Note that this code is just an example and should be customized to fit your specific needs. You should also make sure to follow WordPress best practices and security guidelines when developing plugins.