This is the code for the article:
Click on "Download ZIP" to download the example
Tired of looking for a basic example of a plugin to solve my needs for CRUD operations on a custom table in wordpress I decided to to it myself, and here it is.
This plugin is just a starting point to develop your own management plugin.
This example works with a custom table in the wordpress database. it’s written with simplicity in mind so you can understand everithing quickly.
Includes the following files
- init – plugin initialization, where everything is put toghether
- list – showing a list of items
- update – for updating and deleting items
- create – for inserting new items
- style-admin.css – stylesheet to use in the admin screens
First, install and use the plugin to understand how it works:
- Download the files
- Unzip to wp-content/plugins folder
- Create the table manually on the same wordpress database using the file example-database.sql (you can use the phpmyadmin tool)
- Activate the plugin
You will see a new administration menu on your left:
Take your time and perform all operations: select, insert, update and delete
If yout take a look at the code you’ll see that every function has a prefix “sinetiks” and the name of the table “schools“. This is because you need to create a namespace to avoid duplicated function names.
(The name “sinetiks” is the name of my company and “schools” the name of the table to manage)
How to modify the code to manage another table:
- Replace “sinetiks” with your company name and “school” with the table name
- Replace the columns ID and NAME with your columns
- Modify the html forms
To learn how to use the wordpress database functions: http://codex.wordpress.org/Class_Reference/wpdb
The code is written with the minimum to avoid complexity.
Remember that the purpose of this code is to help you build your own plugin with validations, style, proper messages, etc.
Good luck!