Additional tables in WordPress database - php

I am using WordPress and I would like to develop more services for my website. I am thinking of creating new tables inside WordPress database. Will this approach work? I don't want WordPress to delete my tables while updating etc.
These services will be on separate pages, they will not be WordPress plugins. I just want to use the WordPress database to store my tables.

As referenced by a WP Tech Ninja (Samuel Wood, aka Otto) in this post on wordpress.org, you can safely add tables to the Wordpress database and it shouldn't be an issue for upgrades or maintenance.
Adding new tables is fine and several plugins do just that. Twitter Tools, for one.
[...]
Upgrading can modify the database, but if you're using some other table entirely, that table won't be touched by WordPress. It only looks at its own tables.

Don't do this.
I actually got a chance to speak with Matt Mullenweg a few years ago and he advised to avoid any plugin that adds tables like the plague, for the following reasons:
Use the option table(s) (wp_options or wp_N_options for multisite) to store your data—that's what they are there for. They are basically key/value so you can store pretty much anything you want in there. By doing that, you also get the benefit of being able to using the Wordpress api's simple calls to read and write your data, so you don't have to mess with writing data handlers.
Automatic cleanup when your plugin is removed is much easier as well, as you can—again—use the API and those functions are handled for you (yes, there are hooks to tell when this happens for this very reason). Removing tables manually is a pain and is difficult to do when your plugin gets uninstalled.

Related

How remove main table in database in wordpress without losing wordpress site?

How can I remove one table from wordpress tables without losing wordpress site?
I mean the main tables, not the plugin tables.
For example, the comment table or post table - if I remove it from phpmyadmin and then go back to the wordpress site it shows me an error and starts repairing the database. I want to be able to delete for example the comment table or option table or the main table without losing the wordpress site. I want just the user table in the wordpress site, and want to remove the other tables.
How I can edit wordpress core files that don't check the database table or just check if the user table exists?
Thanks.
Best regards
As noted in the comments, WordPress requires all the tables that it creates, even if it doesn't seem like it is using them. You can absolutely build a system that only uses WordPress for authentication and/or authorization (I've built several), but you must accept that the other tables and all of the core code exists.
If instead you just want a secure login and session, I would encourage you to look at a library such as PHP-Auth or even better, Symfony. With the latter you can very easy get an authentication and authorization in place and then move on to your code.
However, if you are dead-set on using WordPress, and you are willing to accept that the other tables must exist even if you aren't using them, here's some code that we've used successfully on many sites. The documentation is pretty good but generally it relies on overriding WordPress's action for template_include which allows you to run whatever file/code you want. We almost always pair that code with this theme whose sole purpose is to force users to login before doing anything.
Explaining everything in there is out of scope for this site, and I would very much encourage you to not use it unless you have a very specific need that cannot be satisfied by using a third-party library.

What is best practise for storing custom data for Wordpress

Context: I need to create a form with a lot of customisations and quite a few fields (20+) for a site and rather than installing a plugin and modifying the hell out of it (or learning the plugin's API) I decided to write my own plugin. Since I'm doing this from scratch the question of storing data had to be answered.
Most people will suggest using a custom post type and meta data rather than a custom table and while I'm familiar with custom post types and taxonomies etc I wonder whether this is best for pure data storage that amounts to a log.
The form is single use and the data I'm storing won't be accessible on the front end of the site and will only be "viewed" on WP-admin and that would most likely just be a single page with a filterable table.
So the question is does this warrant using a custom post type and meta-data or should I just create a table and use the $wpdb class since that simplifies CRUD and still sticks to the "Wordpress way" of doing things.
Reading: This post makes a good case for a custom table, especially since their example is about storing private data, questions of efficiency and privacy arose. This question, answered a few years ago has the opposite opinion but the use-case was different since the data being stored was for public display.
I would highly suggest using a separate table. The WP table postmeta is usually filled with a lot of info from a lot of different plugins, and quite often ends up being the biggest or the biggest table in the database.
Apart from that, if saved in the postmeta table it will always be also partially saved in the posts table, since these two need each other for the information to be connected and complete. So if you were exporting/importing to another database, you would have to get involved in a very unpleasant process where the custom posts need to be the same ID as they were in the last database
Furthermore, the data is very easily accessible if in a seperate table, and should be easy to read even from phpmyadmin and it should be quite easy to code a filterable table using the $wpdb class if you have just basic sql knowledge.All this comes from my recent experience with merging 2 big wordpress websites into one, and having a lot of info saved as postmeta... I really wish most of it were saved in a custom table, as it would make my life much easier.
The only reason for using meta and a custom post type would be that it is faster and easier(at least in my experience). Hope this helps, I'm really interested to see if there are other opinions. Good luck with your project!
Interesting question!
I normally use (as you mentioned) custom post types and taxonomies for such cases. So that would be my way to go, the only thing that I'm bit scared of is where you mentioned "..amounts to a log.."
I'm pretty sure you would be better off using a separate table, it just sucks to work through the postmeta table. At the end if you know how to use $wpdb class you should be as fine!
Looking forward to others input
In most cases it is easy and cool to use a custom post type with meta-data, because it is fast done and you can use all the cool wordpress functions for it.
A case where it is better to use a new table in the database is when you have to save a lot of data, i.e. access log or similar.
WooCommerce (a plugin from the wordpress people) save the products as CPT and all data that belongs as meta-data.

Get WordPress to Read a Custom Database

(Scroll down for question if you don't require background)
For days now I've been working on a project that in simple terms allows users to search an internal company ID and then be shown a load of information relating to that ID (all stored in a SQL db). Just to give you a tiny bit of background, I've always worked with WordPress, I love WP - Custom post types, custom fields, etc - I've always found it to be a good solution for most things.
Now I'm at something of a roadblock. This database that I need a "front-end" to read cannot be changed, it also cannot be moved (/merged with WP tables, etc) as other apps read the same "live" data from it.
I have managed to put together a tiny (sort-of) front end and search function. But I'm still coming across loads of issues, I'm not a pro at PHP/SQL, etc.
My question is:
For me, WordPress seems to to be the best option. How do I get WordPress to "read" from a database entirely separate from WP and "generate" pages based on this data (so that users can search, see info in this custom data, etc), like custom post types/fields but from a db that was never generated via WP?
I understand that a WP table would still be required (but separate) to run the core functions of WP, etc.
Any help would be greatly appreciated!
I think WP is not the best options in this case. Why you don't create a new application using some php/python framework to do that ? It looks like a simple application - just to search App/IDs and show some information.
If you will use WP, I think it will very complicated, because you will have a lot of working making some type of your-sql-version to wp-sql-version and all...
Try some PHP Framework as Laravel( http://laravel.com/) or even Web2py(a simple and good python framework( http://www.web2py.com/). Web2py is really simple and can connect you in your existed database.
Hope it could be useful for you.
The internals of WordPress are heavily customized for the specific databases that WordPress is designed for. Although you could probably force it to do what you want using various hooks and filters, it would be a lot of work and probably fragile.
The most practical approach would seem to be to have WordPress "mirror" your other database.
1) Create one or more custom post types for the type of information contained in your other database
2) Write a synchronization program that takes data out of the other database and creates posts of the appropriate custom type with the imported data as content (and deletes custom posts that are not longer needed). You would need to have some way (e.g. post slug or custom post metadata) for recording which other DB record a given post came from so you did not create duplicates).
The synchronization program would be quite straightforward, and when you were done you would be able to use all of the standard WordPress features with your other data.
Sounds like you're making it more complex than it needs to be. Use http://codex.wordpress.org/Class_Reference/wpdb to read/write to the database and access the tables and data you want, i.e.
$mydb = new wpdb('username','password','database','localhost');
$rows = $mydb->get_results("select Name from my_table");
echo "<ul>";
foreach ($rows as $obj) :
echo "<li>".$obj->Name."</li>";
endforeach;
echo "</ul>";
and use custom page templates http://codex.wordpress.org/Page_Templates if needed to display both the standard WordPress loop as well as the custom data.
Displaying data from that database is one thing; searching is quite another. In order to search that custom data, you will need to run a search with php just on those tables, since they are not standard Wordpress tables and as such aren't searchable by the WordPress site search functions. See Creating a search form in PHP to search a database? In that case, you will need a custom page template to display the php search form and the results.
Wordpress use EZSQL for database connection and it says that you can create more then one connections.
So your first database connection variable is $wpdb, You can create another with different database
$wpdb1 = new wpdb('username','password','database2','localhost');
Now you can query with second database with varisble $wpdb2.
Read more about EZSQL go to justinvincent.com/ezsql

modx evolution external database

I have 3 seperate modx evolution website. All 3 websites are completely different and therefore run off a seperate database. However for the news and events pages of each website i would like them to all run from the database of the 1 of the 3 websites. I this possible? is there an extra already created for this purpose?
I have looked into the possibility of using an RSS feed, but would rather have them running directly from the database as this would allow a news article to be added from any of the 3 websites.
Thanks,
Sean.
you can create your extra tables in one modx instance & access them externally ~ however they will need to be on the same file system. I am not aware of a method to do this VIA http/API etc. See here.
http://rtfm.modx.com/display/revolution20/Loading+MODx+Externally
What you will want to do is create your extra table and extend modx using xPDO, it's a fair bit of work to get setup initially but worth it in the long run. This is the best example for getting started, although you will not be extending anything [but modx] it does give you useful examples of creating your schema and registering it with modx.
http://rtfm.modx.com/display/revolution20/Extending+modUser
and
http://rtfm.modx.com/display/revolution20/Using+Custom+Database+Tables+in+your+3rd+Party+Components
Finally, there IS an extra that will generate all your schema files for you, however I can't seem to find what it is called... sorry. If you have multiple tables using foreign keys it may not be all that useful to you as it won't create the relationships in the files it generates.

Wordpress library / SQL queries to build new frontend?

I have a wordpress installation I'd like to get rid off. It's been constant work over the past year keeping up with various updates and changes and managing differenet plugins. I've realized I don't need that much functionality and would be perfectly happy with static pages. If I manage to drop the installation I could also speed things up a lot and integrate the content more easily in the rest of our site.
I'm trying to find a PHP or Perl package which I can use to easily query the existing WP DB. Basically, I want to keep the DB and throw away the frontend and backend to WP. Then I just want to add a very minimal layer of PHP or Perl code on top to show the posts in the DB including meta data.
In case there is no package that can make this job easier, is there some concise overview of the SQL queries I will need? I know I can go digging for them in the codebase, but maybe there is someone who has already done this. Googling around didn't immediately turn up useful results.
Maybe you want give a try to jekyll or octopress.

Categories