Yes the fields can be dynamically populated etc.
You need to write a plugin that registers several shortcodes that do various database functions. These shortcodes go into different pages.
Say you have shortcodes called:
– searchanddisplay
– editentry
– addentry
– databaseadd
You create your custom database tables using phpmyadmin etc, at this stage dont bother with creating the tables in code.
This is what each of these shortcode functions would do:
– searchanddisplay
It has a search fields FORM and a results display table. The FORM has a “Search” button that does a submit to the same URL of this page. There could also be pagination controls, the pagination controls are distinct SUBMIT codes.
On entry it test if the search fields are set, if not it sets the search to default like ALL or whatever. It renders the HTML for the form, setting the fields to the received values (or default). It then does an SQL queary, possibly with pagination. If there are no results it says so. Otherwise it generates a table with one result per row, each row has columns for the various fields and a column with an “EDIT” button which is a LINK to the page with the “editentry” shortcode, it also has the key for that row.
– editentry
Tests that the row key is set, complains otherwise.
Tests if it is being invoked by itself using its own submit buttons, does the distinct operations some of which result in a page redirect, otherwise it does an SQL query on that row, displays the results in a form, there are submit action buttons for savechanges, back/cancel and delete.
– addentry
Displays a form, has submit buttons for ADD and cancel.
The ADD action directs to databaseadd page. The cancel links to the searchdisplay page.
– databaseadd
Validates the fields and reports errors, otherwise does an SQL database insert, reports on errors such as duplicate keys.
Note that other organizations are possible.
You can use javascript to provide various support and convenience features (such as “clear search fields”)
When I was getting started I found this plugin to be very handy:
https://wordpress.org/plugins/wp-csv-to-database/
Ross said “Note that other organizations are possible.”
That’s for sure! Shortcodes are certainly a reasonable approach. If you need these forms to appear in various post content, it’s likely the best approach. But if your forms only need to appear in one place, IMO shortcodes add needless complication. I personally would simply build the necessary form code as a PHP page of some sort. If the forms are to appear on the front end, I’d make a custom page template that handles everything. On the backend, I’d make a menu page callback that does the same.
I judge the custom page template the simplest to implement. It’s as close as WP gets to a traditional stand alone PHP file. It’s best to consider what the best user experience would be before deciding what approach to use. What’s easiest shouldn’t be the criteria, but with limited time and/or skill, it becomes a factor.
Yes, I concede that custom page templates present a much more conventional application development context, your comment is a worthwhile contribution to this discussion. Some things are even easier, like I described page redirects, now I think of it I am not sure how I would do this from shortcodes ? Ideas anyone ?
However page templates are an uncomfortable straddle across the realms of theme which controls presentation, and all the rest which controls content. Separating presentation from content is a major theme in the world of the web, html vs css, why you don’t use tables any more to control layout etc. On occasions we have to muddle this separation, like when we target page IDs in css etc, but in general it is a divide worth maintaining.
Building applications in page templates is contrary to the WordPress ecosystem, there are a zillion plugins in the library and elsewhere, I have yet to run across one that incorporates creating an alliance with page templates, which would be a good technique, I just have not seen it.
Thoughts anyone ?