Support » Fixing WordPress » How to access my own MySQL DB from WordPress?

  • I have a separately (from WordPress) designed and developed relational database. Have had no trouble using PHP and HTML to build, use and maintain it on a webserver. What I’d like to be able to do is to incorporate this capability into a website run by WordPress. In that, retain DB functionality but let WordPress handle the appearance of the website.

    This seems like something that would be commonly desired but after spending a considerable amount of time plowing through the enormous amount of WordPress documentation I still haven’t found anything very helpful. If someone could refer me to reference material aimed at addressing the problem from this point of view that would be very helpful.

    • This topic was modified 4 months, 2 weeks ago by Jan Dembowski. Reason: Moved to Fixing WordPress, this is not an Developing with WordPress topic
Viewing 10 replies - 1 through 10 (of 10 total)
  • Moderator Jose Castaneda

    (@jcastaneda)

    THEME COFFEE MONKEY

    Hi there!

    Not sure if you mean connecting to a separate db but that can be done using the $wpdb global. This post sort of goes over that: https://wordpress.stackexchange.com/a/1618

    Thread Starter aajax

    (@aajax)

    Yes! It does look like that article pertains to doing what I want to do and suggests it is possible. However, I notice it is more than 10 years old. While it does suggest I’m NOT the only one who ever wanted to do this it does NOT quite rise to the point of being common.

    I can find the wpdb code reference at WordPress.org. While that is desirable/helpful I think there is more to it than that. For example ->

      Do I need to develop a plugin?
      How to both read/write data dynamically to/from pages/posts?

    I probably should offer that I’m NOT really a WordPress expert but do have experience setting up and running several sites. Have had some success developing a child theme but I do realize there is an awful lot to it that I’ve yet to learn.

    I was hoping that there might be reference documentation. If NOT maybe just some kind of tutorial that might help one to get started.

    • This reply was modified 4 months, 2 weeks ago by aajax.
    Moderator Jose Castaneda

    (@jcastaneda)

    THEME COFFEE MONKEY

    Do I need to develop a plugin?

    Yes, would need to have a plugin. A good method may be using a must-use plugin (https://wordpress.org/support/article/must-use-plugins/).

    As for the read/write portion, WordPress does provide some functions/methods as well which are usually named wp_post_{method} or along those lines. The noice part too being that you can check the reference site for sanity check too ( I do it a lot ) and that’s located here: https://developer.wordpress.org/reference/

    Moderator bcworkz

    (@bcworkz)

    If you already have a child theme, it’s possible for your custom code to reside there instead of a plugin. Having it in a separate plugin might make more organizational sense. Plugins are typically for altering site functionality, themes are for defining site appearance. For one-off code for a specific site you can go either way.

    It sounds like you’re stuck on how to get the data displayed on the site more than getting it from the DB. On a child theme, you could develop a custom page template to display data as you wish. From a plugin, template code is impractical. It’s best to utilize a shortcode or custom block to generate output from plugins.

    Thread Starter aajax

    (@aajax)

    Thanks, the advice regarding theme verses plugin is quite helpful.

    As I said I have it working without WordPress. In that, I do know how to deal with the database using PHP. To the extent that WordPress might prefer using built-in code I would certainly opt to try and do that.

    You are correct about the part I’m most at a loss for which might could be described as how a page (or post) interacts with the database. This includes queries that both fetch & update data stored in the database. In that, some kind of FORM (? terminology)) that supports fields for data entry are needed as well as replying with results. Something I’ve noticed more recently is the emphasis WordPress is placing on the so-called Block Editor. Question might be can it be used to produce interactive pages? If so how? If NOT how?

    Moderator bcworkz

    (@bcworkz)

    The wpdb class can be used to connect to any mySQL DB. There’s already an instance of that class in the global $wpdb object which is already connected to the WP DB. You can add custom tables to the DB and interact with it through this connection object.
    https://developer.wordpress.org/reference/classes/wpdb/

    It’s possible to create custom editor blocks that do nearly anything you could conceive of in code. Refer to the Block Editor Handbook. An older alternative that can achieve much of the same are shortcodes. Another possible alternative are custom page templates.

    Thread Starter aajax

    (@aajax)

    Yes, based on discussion to date I have been researching how to develop a plugin that would solve my problem. The wpdb class adresses the the database interface part and shortcodes appear to provide the means to dynamically supply content for display on a page. The part I’m still looking for would pertain to how the data included in an HTTP POST request is retrieved. In that, how to obtain the data entered into an HTML form that I think can be requested via the shortcode mechanism. Being able to associate/acknowledge receipt by sending a status reply would also be desired.

    Something that would be a big help would be some kind of sample plugin. While there is no shortage of plugins to install and then try and review, my idea would be a minimal plugin that includes only the necessary files but does something simple that can be associated with the code. Might there be something like that supplied by/for WordPress developers. If not maybe there is a well known plugin that somewhat fits my idea of only including basic necessary features that could be recommended for this purpose.

    • This reply was modified 4 months, 2 weeks ago by aajax.
    Moderator bcworkz

    (@bcworkz)

    Once the POST request reaches the server, you of course get the passed data from $_POST just like on any other site. Where it gets WP specific is where to route the request (the form’s action attribute) so your code can process the passed data. If your code is pure PHP, no WP resources are involved, the request could go straight to your .php code page.

    Of course, wpdb is a WP resource. To get your code that uses it to process form data, you’ll need the WP environment initialized. There are limited ways to do this. Your options are to send requests to admin-ajax.php, admin-post.php, or a page using a custom page template. Or let the current page reload and intercept the request via an action hook or equivalent. The form processing code could then be part of the shortcode handler function since the shortcode is presumably on the current page. The best choice depends on what sort of user experience you want after the form is submitted.

    For existing plugins to use as examples and guidance, maybe look at one of the custom forms plugins?

    Thread Starter aajax

    (@aajax)

    Thanks! I think that provides what I need to get started.

    Just in case anybody else cares, I thought it might be worth including some links I found as a result of this discussion. While I have NOT yet produced anything that verifies the accuracy of the referenced material, my initial reading finds them a bit more thorough than others that I found. Consider the following:

    https://www.sitepoint.com/working-with-databases-in-wordpress/ -> Provides more detail about accessing a database than any others that I found.

    https://wpmudev.com/blog/handling-form-submissions/ -> Provides what looks to be a thorough explanation of form submission. This article also includes references to other material of interest as follows:

    https://github.com/karannagupta/nds-admin-form-demo -> A demonstration plugin on which the article is based, which can be installed and run on a test system,

    https://github.com/DevinVinson/WordPress-Plugin-Boilerplate -> A reference to the material on the WordPress site that fits my idea of code that illustrates how it is done which also conforms to recommended coding practices. The demo plugin claims to conform.

    It will take me a while to work through these materials, However, I’d like to thank respondents for their help. Should I find issues with the above I’ll try and remember to update this post.

    Many thanks!

    • This reply was modified 4 months, 2 weeks ago by aajax.
    • This reply was modified 4 months, 2 weeks ago by aajax.
    Moderator Jose Castaneda

    (@jcastaneda)

    THEME COFFEE MONKEY

    That’s awesome and happy to have been of some help here.

Viewing 10 replies - 1 through 10 (of 10 total)
  • You must be logged in to reply to this topic.