Skip to content

Tutorial - Custom Database Tables #1867

Closed

Description

@jonathanbossenger

Topic Description

While the default WordPress database schema is perfect for most content-related data types, there may be instances where you need to create custom tables in a WordPress database. One example of this is when a plugin has a specific use case that extends beyond what the custom post-type functionality of WordPress offers. In this tutorial, you will learn how to create and manage custom WordPress database tables, how to interact with these tables, and some maintenance considerations.

Related Resources

Links to related content on Learn, HelpHub, DevHub, GitHub Gutenberg Issues, DevNotes, etc.

Guidelines

Review the team guidelines

Tutorial Development Checklist

  • Vetted by instructional designers for content idea
  • Provide feedback of the idea
  • Gather links to Support and Developer Docs
  • Review any related material on Learn
  • Define several SEO keywords to use in the article and where they should be prominently used
  • Description and Objectives finalized
  • Tutorial created and announced to the team for Q/A review
  • Tutorial reviewed and ready to publish
  • Tutorial submitted and published to WPTV
  • Tutorial published on WPTV
  • Tutorial captioned
  • Tutorial created on Learn.WordPress.org
  • Tutorial post reviewed for grammar, spelling, etc.
  • Tutorial published on Learn.WordPress.org
  • Tutorial announced to Marketing Team for promotion

Activity

  1. moved this from 👋 Ready to Create to 🚧 Drafts in Progress in LearnWP Content - Developmenton Sep 15, 2023
  2. added and removed
    Awaiting TriageIssues awaiting triage. See Training Team handbook for how to triage issues.
    on Sep 15, 2023
  3. SierraTR commented on Sep 19, 2023

    @SierraTR

    You might consider making the title more specific - such as Custom Database tables - so that it is not confused with html tables, the table block in site content or selecting a table-specific plugin. Or direct it toward developers rather than users.

  4. changed the title [-]Tutorial - Custom Tables[/-] [+]Tutorial - Custom Database Tables[/+] on Sep 20, 2023
  5. jonathanbossenger commented on Sep 20, 2023

    @jonathanbossenger
    CollaboratorAuthor

    That's a good point @SierraTR, edited.

  6. jonathanbossenger commented on Sep 26, 2023

    @jonathanbossenger
    CollaboratorAuthor

    WIP video for review:

    Custom.Database.Tables.mp4

    In preparing this video for review, I realized there are two fixes I need to make

    1. at 5:06, in the example code to update the record, the ID field should be id
    2. at 6:37 I say plugin install not plugin uninstall

    I will make sure to fix those before the video is published.

  7. TinaCollier commented on Sep 26, 2023

    @TinaCollier

    Hello @jonathanbossenger ,

    I thoroughly enjoyed your tutorial and here is what I noted during the video:

    1. You clearly and concisely met all objectives in this video including Where to find information about creating custom database tables, How to create custom database tables, and How to interact with these custom database tables.
    2. Your example code and the resources mentioned in the video were very helpful in understanding the process of creating custom database tables. I was easily able to follow along with your explanations and points
    3. At the end of the video, you quickly reviewed table updates. I appreciate that you mentioned this process.

    Great tutorial and I wouldn't change a thing about it other than the two items you mentioned above.

  8. digitalchild commented on Sep 27, 2023

    @digitalchild
    Contributor

    Great tutorial @jonathanbossenger my only question is why did the custom table docs not get migrated from codex?

  9. 13 remaining items

  10. jonathanbossenger commented on Oct 3, 2023

    @jonathanbossenger
    CollaboratorAuthor

    Thanks for your review @Zebedeu

    It is important to emphasize and explain to the user that certain practices were used only to illustrate a concept, but are not recommended.

    I'm just checking, do you feel that this needs to be added, or that this idea is already covered in the tutorial?

  11. Zebedeu commented on Oct 3, 2023

    @Zebedeu

    Yes, this information is important and should be included in the tutorial. It is essential to emphasize to the user that when creating custom tables in WordPress, it is crucial to version them to control and update the table structure if necessary. Additionally, it is important to note that this technique allows for updates to be applied consistently without compromising the integrity of the stored data in the table. Therefore, versioning is a valuable and indispensable resource for managing custom tables in WordPress.

  12. jonathanbossenger commented on Oct 3, 2023

    @jonathanbossenger
    CollaboratorAuthor

    @Zebedeu thanks for your feedback.

    As I mentioned to another reviewer in an earlier comment I do think that a follow-up tutorial on custom tables would be good here:

    So I do want to dive more into custom tables in the future, including something about how one might manage the actual upgrade routine, what to do if queries fail, etc.

    The problem exists in balancing the limitation of creating a tutorial video 5-10 minutes long, keeping the content engaging and easier to follow for viewers, without trying to rush through all the available information about a specific topic.

    Quite honestly, the topic of custom tables could be a series of tutorials, even a short course on the subject.

    So I just want to make sure I understand your specific review feedback here, as I think there are two action items:

    1. explain to the user that certain practices were used only to illustrate a concept, but are not recommended.
    2. add details on table versioning

    Would you agree that a follow-up tutorial (or tutorials) including things like versioning, what to do if queries fail, and other topics related to custom tables would make sense?

    Additionally, if you're interested, this might be content you or another contributor might be interested in working on.

  13. jonathanbossenger commented on Oct 3, 2023

    @jonathanbossenger
    CollaboratorAuthor

    @ryanwelcher for the "if table exists check" on inserting data, would you be happy with using get_var and a SHOW TABLES MySQL statement?

        function insert_record_into_table(){
            global $wpdb;
    
            $table_name = $wpdb->prefix . 'custom_table';
    
            $table_exists = $wpdb->get_var( "SHOW TABLES LIKE '$table_name'" );
    
            if ( $table_exists ) {
                $wpdb->insert(
                    $table_name,
                    array(
                        'time' => current_time( 'mysql' ),
                        'name' => 'John Doe',
                        'text' => 'Hello World!',
                        'url'  => 'https://wordpress.org'
                    )
                );
            }
        }

    $table_exists would either be the table name, or NULL if it doesn't exist.

  14. ryanwelcher commented on Oct 3, 2023

    @ryanwelcher
    Collaborator

    for the "if table exists check" on inserting data, would you be happy with using get_var and a SHOW TABLES MySQL statement?

    Explaining the why here is probably more important than the implementation but that looks great to me and demonstrates the steps perfectly.

  15. jonathanbossenger commented on Oct 3, 2023

    @jonathanbossenger
    CollaboratorAuthor

    Thanks @ryanwelcher.

    I'm wondering now if this item, as well as the items about when to create tables and when to delete them, should be in the follow-up tutorial.

    What I'm thinking is:

    This tutorial = the basics of creating, inserting data, updating, and deleting custom tables (aka the theory)

    Follow-up tutorial = how you would actually implement this with real-world examples (aka in practice)

    1. Adding tables on plugin activation and removing them only on uninstall
    2. Using the table exists check on inserts and updates. Thinking about this more, running a table exists check every time you insert or update data isn't super performant, maybe an option stored once the table is created is better, although this doesn't solve the problem if the table is deleted some other way. That being said...
    3. Implementing proper checks for all queries, with error output to the user if any of them fail. This would prevent the problem at 2) because then at least it can fail gracefully and report an error.
    4. When, and how, to use prepare with query for a custom insert or delete (although I can't think of a real-world example at this time)
    5. How to implement the upgrade routine, to manage table updates

    This would be similar to another two-part series I did recently on plugin vulns:

    1. https://learn.wordpress.org/tutorial/introduction-to-securely-developing-plugins/
    2. https://learn.wordpress.org/tutorial/extending-wordpress-common-security-vulnerabilities/

    I'm thinking of this in terms not only of being able to create courses based on this content, where one small step follows the next, but also in terms of the learning pathways project.

    Thoughts?

  16. ryanwelcher commented on Oct 5, 2023

    @ryanwelcher
    Collaborator

    @jonathanbossenger that sounds like a good approach to me. It would be very easy to overwhelm the user with all of the nuances in a single tutorial and splitting them out mitigates that and allows you to spend more time on each.

  17. jonathanbossenger commented on Oct 5, 2023

    @jonathanbossenger
    CollaboratorAuthor

    Thank you @ryanwelcher I appreciate your experienced feedback on this topic.

  18. jonathanbossenger commented on Oct 13, 2023

    @jonathanbossenger
    CollaboratorAuthor
  19. added a commit that references this issue on Oct 13, 2023
  20. moved this from 🚧 Drafts in Progress to 📜 Published or Closed in LearnWP Content - Developmenton Oct 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

hacktoberfestTo mark issues for the Hacktoberfest event each October.

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions